How to native convert string -> base64 and base64 -> string
I\'m find only this bytes to base64string
would like this:
You can use the BASE64 codec (renamed base64 in Dart 2) and the LATIN1 codec (renamed latin1 in Dart 2) from convert library.
var stringToEncode = 'Dart is awesome';
// encoding
var bytesInLatin1 = LATIN1.encode(stringToEncode);
// [68, 97, 114, 116, 32, 105, 115, 32, 97, 119, 101, 115, 111, 109, 101]
var base64encoded = BASE64.encode(bytesInLatin1);
// RGFydCBpcyBhd2Vzb21l
// decoding
var bytesInLatin1_decoded = BASE64.decode(base64encoded);
// [68, 97, 114, 116, 32, 105, 115, 32, 97, 119, 101, 115, 111, 109, 101]
var initialValue = LATIN1.decode(bytesInLatin1_decoded);
// Dart is awesome
If you always use LATIN1 to generate the encoded String you can avoid the 2 convert calls by creating a codec to directly convert a String to/from an encoded String.
var codec = LATIN1.fuse(BASE64);
print(codec.encode('Dart is awesome'));
// RGFydCBpcyBhd2Vzb21l
print(codec.decode('RGFydCBpcyBhd2Vzb21l'));
// Dart is awesome