Want to convert BASE64 string into Image with Flutter? You can convert a Uint8List to a Flutter Image widget using the Image.memory constructor. (Use the Uint8List.fromList constructor to convert a List to Uint8List if necessary.) You can use BASE64.encode to go the other way.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | import 'dart:async'; import 'dart:convert'; import 'dart:typed_data'; import 'package:flutter/material.dart'; import 'package:http/http.dart' as http; void main() { runApp(new MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return new MaterialApp( theme: new ThemeData.dark(), home: new MyHomePage(), ); } } class MyHomePage extends StatefulWidget { @override State createState() => new MyHomePageState(); } class MyHomePageState extends State<MyHomePage> { String _base64; @override void initState() { super.initState(); (() async { http.Response response = await http.get( 'https://flutter.io/images/flutter-mark-square-100.png', ); if (mounted) { setState(() { _base64 = BASE64.encode(response.bodyBytes); }); } })(); } @override Widget build(BuildContext context) { if (_base64 == null) return new Container(); Uint8List bytes = BASE64.decode(_base64); return new Scaffold( appBar: new AppBar(title: new Text('Example App')), body: new ListTile( leading: new Image.memory(bytes), title: new Text(_base64), ), ); } } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.