Want to load image from Firebase Storage in Flutter? Here’s an example of a stateful widget that loads an image from Firebase Storage object and builds an Image object:
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 | class _MyHomePageState extends State<MyHomePage> { final FirebaseStorage storage = FirebaseStorage( app: Firestore.instance.app, storageBucket: 'gs://my-project.appspot.com'); Uint8List imageBytes; String errorMsg; _MyHomePageState() { storage.ref().child('selfies/me2.jpg').getData(10000000).then((data) => setState(() { imageBytes = data; }) ).catchError((e) => setState(() { errorMsg = e.error; }) ); } @override Widget build(BuildContext context) { var img = imageBytes != null ? Image.memory( imageBytes, fit: BoxFit.cover, ) : Text(errorMsg != null ? errorMsg : "Loading..."); return new Scaffold( appBar: new AppBar( title: new Text(widget.title), ), body: new ListView( children: <Widget>[ img, ], )); } } |
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.