乐闻世界logo
搜索文章和话题

How to convert BASE64 string into Image with Flutter?

1个答案

1

The process involves the following steps:

  1. Import the Dart convert library: This library provides methods for data encoding and decoding, including BASE64.
  2. Decode the BASE64 string: Convert the BASE64-encoded string into a Uint8List, which represents the image bytes as a list of bytes.
  3. Use the Image.memory widget to display the image: Pass the decoded byte data to Image.memory to render the image.

Here is a specific implementation example:

dart
import 'dart:convert'; import 'package:flutter/material.dart'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { Widget build(BuildContext context) { // Example BASE64 string, typically obtained from the network or other sources String base64String = 'iVBORw0KGgoA...'; // Decode the BASE64 string Uint8List bytes = base64.decode(base64String); return MaterialApp( home: Scaffold( appBar: AppBar( title: Text('BASE64 Image Example'), ), body: Center( // Display the image child: Image.memory(bytes), ), ), ); } }

In this example:

  • First, we import the necessary libraries;
  • Using the base64.decode method, convert the BASE64 string base64String into a Uint8List named bytes;
  • Finally, use the Image.memory widget to render these bytes as an image.

This approach is particularly useful when dealing with encoded images from the network, databases, or user uploads. You can similarly handle other encoded data types.

2024年8月8日 01:13 回复

你的答案