Search code examples
firebaseflutterfirebase-storagestream-builder

Streambuilder help flutter firebase storage


Hello I’m trying to make a sequence of images with flutter but when I update the photo in firebase Storage the photo in my APP remains the same. Maybe I can do this with streambuilder but I don’t know how to use it and I can’t learn how to use it. Does anyone know how I can do that thanks so much for the help.

class _MyHomePageState extends State<MyHomePage> {

  final FirebaseStorage storage = FirebaseStorage(
      app: Firestore.instance.app,
      storageBucket: 'gs:...com/');

  Uint8List imageBytes;
  String errorMsg;

  _MyHomePageState() {
    storage.ref().child('images/opencv.png').getData(100000000000000).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(
        ),
        body: new ListView(
          children: <Widget>[
            img,
          ],
        ));
  }
}

Solution

  • Firebase Storage does not automatically notify clients when there's a change to an image they loaded. When you call getData() it gets the data once, and that' the end of it.

    If you want to auto-reload the image when it changes in Storage, I recommend using another service to handle that notification.

    For example, you could use Firebase Realtime Database or Cloud Firestore to store the path of the image and when it was last modified, and then update that at the same time when you update the image data in Storage. Then your client code would use a realtime listener to the database, and from that load/reload the image.