dart base64 encode

It requires a few steps, but encoding a string in base64 is pretty straightforward.

Dart has a function in the package:crypto library, CryptoUtils.bytesToBase64, which takes a list of bytes to encode as base64. In order to get the list of bytes from a Dart string, you can use the UTF8.encode() function in the dart:convert library.

All together, this looks like:

import 'dart:convert';
import 'package:crypto/crypto.dart';

main() {
  var str = "Hello world";
  var bytes = UTF8.encode(str);
  var base64 = CryptoUtils.bytesToBase64(bytes);
  print(base64);
}
If you're working inside the browser, then you have the easier option of using the browser's built in btoa function. The above code snippet becomes:

import 'dart:html';

main() {
  var str = "Hello world";
  var base64 = window.btoa(str);
  print(base64);
}

Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source