How to Convert String to Base64/Base64Url Encoding in Flutter

In this example, we are going to show you how to convert string to base64 or base64Url encoding string in Flutter. This method can be used for two-way encryption of sensitive data in Flutter. See the example below:

Import this library for the base64 encoding and decoding:

import 'dart:convert';

String mydata = "myusername|password";
String bs64 = base64.encode(mydata.codeUnits);
print(bs64); //output: bXl1c2VybmFtZXxwYXNzd29yZA==

You can use the following code too:

String mydata1 = "myusername|password";
List<int> mydataint = utf8.encode(mydata1);
String bs64str = base64.encode(mydataint);
print(bs64str); //output: bXl1c2VybmFtZXxwYXNzd29yZA==

List<int> decodedint = base64.decode(bs64string);
String decodedstring = utf8.decode(decodedint);

See this also: How to Convert Image to Base64 Encoding in Flutter/Dart

String mydata2 = "myusername|password";
String bs64str1 = base64Url.encode(mydata1.codeUnits);
print(bs64str1); //output: bXl1c2VybmFtZXxwYXNzd29yZA==

List<int> decodedint1 = base64Url.decode(bs64str1);
String decodedstring1 = utf8.decode(decodedint1);

In this way, you can convert String to Base64 or Base64Url encoding in Flutter.

No any Comments on this Article


Please Wait...