Flutter Load Image from Firebase Storage

前端 未结 3 568
盖世英雄少女心
盖世英雄少女心 2020-12-05 18:29

I see there are a lot of examples on how to upload an image using flutter to firebase storage but nothing on actually downloading/reading/displaying one that\'s already bee

3条回答
  •  时光说笑
    2020-12-05 19:11

    Here's an example of a stateful widget that loads an image from Firebase Storage object and builds an Image object:

    class _MyHomePageState extends State {
    
      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: [
                img,
              ],
            ));
      }
    }
    

    Note that FirebaseApp initialization is handled by the Firestore class, so no further initialization code is necessary.

提交回复
热议问题