Base64 Encode & Decode in Dart & Flutter
Encode or decode below — it runs locally in your browser — then grab the canonical Dart code. Dart's built-in dart:convert library works on bytes, so you utf8.encode() a string before encoding and utf8.decode() the result after. No package needed.
Encode and decode a string
The key pattern is String → utf8.encode() → base64Encode() → base64Decode() → utf8.decode() → String:
import 'dart:convert';
// Encode
final encoded = base64Encode(utf8.encode("Hello, world!"));
print(encoded); // SGVsbG8sIHdvcmxkIQ==
// Decode
final decoded = utf8.decode(base64Decode("SGVsbG8sIHdvcmxkIQ=="));
print(decoded); // Hello, world!
Tip: Base64 in Dart operates on bytes (List<int>), never on strings directly. Always go through utf8.encode / utf8.decode so non-ASCII text and emoji round-trip correctly.
URL-safe Base64
For JWTs, URLs, and filenames, use the URL-safe alphabet (- and _ instead of + and /) via the base64Url codec from dart:convert:
import 'dart:convert';
final bytes = utf8.encode("subjects?_d=qux");
// URL-safe encode
final urlSafe = base64Url.encode(bytes);
// URL-safe decode
final decoded = base64Url.decode(urlSafe);
Encode an image (Flutter)
In Flutter, images arrive as a Uint8List of bytes. Since Uint8List is a List<int>, pass it straight to base64Encode:
import 'dart:convert'; import 'dart:io'; // From a file on disk final encoded = base64Encode(File(path).readAsBytesSync()); // Or from a Uint8List you already hold final dataUri = "data:image/png;base64,$encoded";
Frequently asked questions
How do I Base64 encode a string in Dart?
Encode the string to bytes, then Base64-encode: base64Encode(utf8.encode("Hello, world!")) returns "SGVsbG8sIHdvcmxkIQ==".
How do I Base64 decode in Dart?
base64Decode("SGVsbG8sIHdvcmxkIQ==") returns the bytes; add utf8.decode(...) for a string.
What is the difference between base64 and base64Url in Dart?
base64 uses + and /; base64Url uses the URL-safe - and _. Both are in dart:convert with no extra package.
How do I encode an image to Base64 in Flutter?
Read the image as bytes and pass it to base64Encode: base64Encode(File(path).readAsBytesSync()), then build a data:image/png;base64,... URI.
Need image, file, or URL-safe modes?
The main base64.dev tool handles text, images, files, and URL-safe Base64 with auto-detect.
Open base64.dev →