How to convert BASE64 string into Image with Flutter?

CodeWithFlutter
1 Min Read

You can convert a BASE64 string into an image in Flutter by using the base64Decode method from the dart:convert library to convert the BASE64 string into a Uint8List, and then using the Image.memory constructor to create an image widget from the binary data.

Here’s an example:

import 'dart:convert';
import 'package:flutter/widgets.dart';

class Base64Image extends StatelessWidget {
  final String base64Image;

  Base64Image({this.base64Image});

  @override
  Widget build(BuildContext context) {
    return Image.memory(
      base64Decode(base64Image),
      fit: BoxFit.cover,
    );
  }
}

In this example, the Base64Image class takes a base64Image argument in its constructor and creates an Image widget from the binary data decoded from the BASE64 string using the base64Decode method. The fit property is used to specify how the image should fit into its parent container.

You can use the Base64Image widget in your Flutter app like this:

Base64Image(
  base64Image: "iVBORw0KG...",
),

Note that the BASE64 string in this example is just an example and you should replace it with your own BASE64 string.

Share this Article
Leave a comment
adbanner