Base64 encoding in Flutter

Base64encoding in Flutter and Dart

Dart's convert library provides the functionality of encode and decode in flutter . It provides functionality to encode binary data to Base64 format and decode such encodings vice versa.

import 'dart:convert';

Encode string to base64 in Flutter

In order to encode a string to base64 we are going to use same library , first we encode using utf8.encode then we encode to base 64 using base64.encode.

  String text = "base64encode.tech";
  List encodeUTF = utf8.encode(text);
  String base64Text = base64.encode(encodeUTF);
  print('base64: $base64Text');

there is another way of doing it is using fuse , one of the advantage of using fuse is it is null safety and functionally it is same as above it just guranttes we do a UTF8 encoding before converting to base64. Here is how you do it using fuse.

String text = "base64encode.tech";
Codec<String, String> stringToBase64 = utf8.fuse(base64);
String encoded = stringToBase64.encode(text);
print('base64: $encoded');