How to Encode And Decode Base64 and Base64Url In Flutter?

· 3 min read
Encode And Decode Base64 and Base64Url In Flutter
Encode And Decode Base64 and Base64Url In Flutter

Base64 is ubiquitous on the Internet right now. Sometimes it seems like every request, every URL, and every file is being encoded in base64 format! As a programmer, base64 certainly feels like a daily fact of life. So in today’s article, We will take a dive into learning how to Encode And Decode Base64 and Base64Url In Flutter.

Base64 is a binary to ASCII encoding scheme. So it is designed as a way to transfer binary data reliably across channels that have limited support for different content types.

A base64 encoded string looks like this:


Base64 characters only use the same 64 characters that are present in most character sets.

How to Encode And Decode Base64 and Base64Url In Flutter ??

The dart:convert library contains an encoder and decoder for Base64 and Base64Url. However, they encode and decode Lists of integers, so for strings you also need to encode and decode in UTF-8. Rather than doing these two encodings separately, you can combine them with a fuse.

So you need to have the following import:

import 'dart:convert';
String credentials = "username:password";
Codec<String, String> stringToBase64 = utf8.fuse(base64);
String encoded = stringToBase64.encode(credentials);      // dXNlcm5hbWU6cGFzc3dvcmQ=
String decoded = stringToBase64.decode(encoded);

Note that this is equivalent to:

String encoded = base64.encode(utf8.encode(credentials)); // dXNlcm5hbWU6cGFzc3dvcmQ=
String decoded = utf8.decode(base64.decode(encoded));     //
String credentials = "username:password";
Codec<String, String> stringToBase64Url = utf8.fuse(base64Url);
String encoded = stringToBase64Url.encode(credentials);      // dXNlcm5hbWU6cGFzc3dvcmQ=
String decoded = stringToBase64Url.decode(encoded);

So again, this is equivalent to:

String encoded = base64Url.encode(utf8.encode(credentials)); // dXNlcm5hbWU6cGFzc3dvcmQ=
String decoded = utf8.decode(base64Url.decode(encoded));     /
See also

Decoding base64

To decode base64, you simply have to reverse the above operation:

  • First, you remove any padding characters from the end of the encoded string.
  • Then, you translate each base64 character back to their six-bit binary representation.
  • Finally, you divide the bits into byte-sized (eight-bit) chunks and translate the data back to its original format.


Let me know how do you encode and decode Base64 and Base64Url in flutter in the comments below.

Thanks for being with us on a Flutter Journey !!!

Keep Learning !!! Keep Fluttering !!!

So if you are still facing any problems in Flutter development?? Let us know and we will help you.

Flutter Agency is our portal Platform dedicated to Flutter Technology and Flutter Developers. The portal is full of cool resources from Flutter like Flutter Widget GuideFlutter ProjectsCode libs and etc.

Flutter Agency is one of the most popular online portals dedicated to Flutter Technology and daily thousands of unique visitors come to this portal to enhance their knowledge of Flutter.

Leave a Reply