list - How to convert BASE64 string into Image with Flutter? -
i'm converting images saved in firebase database base64 , decode , encode. i've researched similar questions, still getting errors. here have far?
var image1 = string; var pic = event.snapshot.value['image']; var photo = base64.decode(pic); image1 = photo;
i'm getting following error...
a value of type "list<int>" cannot assigned variable of type "type"
if please provide reverse process encoding image base64 may saved firebase, appreciated.
*** update
here updated code that's still throwing error.
image1 = event.snapshot.value['image']; var image = base64.decode(image1.tostring()); new image.memory(image),
the error is...
formatexception: invalid length must multiple of 4
you can convert uint8list
flutter image
widget using image.memory
constructor. (use uint8list.fromlist
constructor convert list
uint8list
if necessary.) can use base64.encode
go other way.
here's sample code.
import 'dart:async'; import 'dart:convert'; import 'dart:typed_data'; import 'package:flutter/material.dart'; import 'package:http/http.dart' 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), ), ); } }
however, it's bad idea store large blobs of binary data in database. it's not playing strengths of firebase realtime database , end wasting bandwidth transmitting data don't need unnecessary encoding , decoding. should use firebase_storage
plugin instead, storing path or download url of image in database.
Comments
Post a Comment