Try it yourself with our free Base64 tool — runs entirely in your browser, no signup needed.

How to Base64 decode in Dart

How to Base64 decode in Dart

Base64 decoding is a crucial operation in many applications, especially when dealing with data exchange between systems. In Dart, Base64 decoding can be achieved using the dart:convert library. This guide will walk you through the process of Base64 decoding in Dart, covering the most common use case, edge cases, and performance tips.

Quick Example

Here is a minimal example of how to Base64 decode a string in Dart:

import 'dart:convert';

void main() {
  String encodedString = 'SGVsbG8gd29ybGQh';
  String decodedString = utf8.decode(base64Decode(encodedString));
  print(decodedString); // Output: Hello world!
}

This example assumes you have the dart:convert library imported. If you're using a Dart package, you can add the convert dependency to your pubspec.yaml file:

dependencies:
  convert: ^3.0.0

Then, run dart pub get to install the dependency.

Step-by-Step Breakdown

Let's break down the example code:

  1. import 'dart:convert';: We import the dart:convert library, which provides the base64Decode function.
  2. String encodedString = 'SGVsbG8gd29ybGQh';: We define a Base64-encoded string.
  3. String decodedString = utf8.decode(base64Decode(encodedString));: We use the base64Decode function to decode the Base64-encoded string. The utf8.decode function is used to convert the decoded bytes to a string.
  4. print(decodedString);: We print the decoded string.

Handling Edge Cases

Empty/null input

When dealing with empty or null input, you should handle it explicitly to avoid runtime errors:

String? encodedString = null;
if (encodedString != null) {
  String decodedString = utf8.decode(base64Decode(encodedString));
  print(decodedString);
} else {
  print('Input is empty or null');
}

Invalid input

Invalid input, such as a string that is not a valid Base64-encoded string, will throw a FormatException. You can handle this exception using a try-catch block:

try {
  String encodedString = 'InvalidBase64';
  String decodedString = utf8.decode(base64Decode(encodedString));
  print(decodedString);
} catch (e) {
  print('Invalid input: $e');
}

Large input

When dealing with large input, you may want to consider using a streaming approach to avoid loading the entire input into memory. The base64Decode function returns a Uint8List, which can be processed in chunks:

import 'dart:convert';
import 'dart:typed_data';

void main() {
  String encodedString = '...large Base64-encoded string...';
  Uint8List decodedBytes = base64Decode(encodedString);
  for (int i = 0; i < decodedBytes.length; i += 1024) {
    Uint8List chunk = decodedBytes.sublist(i, i + 1024);
    String decodedChunk = utf8.decode(chunk);
    print(decodedChunk);
  }
}

Unicode/special characters

Base64 decoding does not handle Unicode or special characters explicitly. However, the utf8.decode function will handle Unicode characters correctly:

String encodedString = 'SGVsbG8gd29ybGQhIC0g4oCM'; // Hello world! - 
String decodedString = utf8.decode(base64Decode(encodedString));
print(decodedString); // Output: Hello world! - 

Common Mistakes

1. Not handling null/empty input

// Wrong
String decodedString = utf8.decode(base64Decode(encodedString));

// Correct
if (encodedString != null) {
  String decodedString = utf8.decode(base64Decode(encodedString));
  print(decodedString);
} else {
  print('Input is empty or null');
}

2. Not handling invalid input

// Wrong
String decodedString = utf8.decode(base64Decode(encodedString));

// Correct
try {
  String decodedString = utf8.decode(base64Decode(encodedString));
  print(decodedString);
} catch (e) {
  print('Invalid input: $e');
}

3. Not using utf8.decode to convert bytes to string

// Wrong
String decodedString = base64Decode(encodedString).toString();

// Correct
String decodedString = utf8.decode(base64Decode(encodedString));

Performance Tips

1. Use base64Decode with a Uint8List instead of a String

// Less efficient
String decodedString = utf8.decode(base64Decode(encodedString));

// More efficient
Uint8List decodedBytes = base64Decode(encodedString);
String decodedString = utf8.decode(decodedBytes);

2. Avoid using base64Decode with large input

Instead, use a streaming approach to process the input in chunks.

3. Use utf8.decode to convert bytes to string

Using utf8.decode is more efficient than using toString() on the decoded bytes.

FAQ

Q: What is the difference between base64Decode and utf8.decode?

A: base64Decode decodes a Base64-encoded string to a Uint8List, while utf8.decode converts a Uint8List to a string.

Q: How do I handle invalid input?

A: Use a try-catch block to catch the FormatException thrown by base64Decode.

Q: Can I use base64Decode with Unicode characters?

A: Yes, utf8.decode will handle Unicode characters correctly.

Q: How do I optimize performance when decoding large input?

A: Use a streaming approach to process the input in chunks.

Q: What is the best way to convert bytes to a string?

A: Use utf8.decode to convert bytes to a string.

AI agent tools available. The CodeTidy MCP Server gives Claude, Cursor, and other AI agents access to 60+ developer tools. One command: npx @codetidy/mcp