Base64 Encoding in Dart
Dart provides built-in support for base64 encoding/decoding.
In Dart, we use the dart:convert
library which provides base64 encoding and decoding functionality. The base64
class is used for standard base64 encoding, while base64Url
is used for URL-compatible encoding.
The string encodes to slightly different values with the standard and URL base64 encoders (trailing +
vs -
), but they both decode to the original string as desired.
To run this Dart program, save it as base64_encoding.dart
and use the following command:
Note that in Dart, we use utf8.encode()
to convert the string to a list of bytes before encoding, and utf8.decode()
to convert the decoded bytes back to a string. Also, Dart’s base64 decoding methods may throw a FormatException
if the input is not properly formatted, so it’s good practice to handle this potential exception.