The process involves the following steps:
- Import the Dart convert library: This library provides methods for data encoding and decoding, including BASE64.
- Decode the BASE64 string: Convert the BASE64-encoded string into a
Uint8List, which represents the image bytes as a list of bytes. - Use the
Image.memorywidget to display the image: Pass the decoded byte data toImage.memoryto render the image.
Here is a specific implementation example:
dartimport '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.decodemethod, convert the BASE64 stringbase64Stringinto aUint8Listnamedbytes; - Finally, use the
Image.memorywidget 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 回复