简体   繁体   中英

How to fetch all images from a folder in Firebase Storage?

I'm working on an story view application in which I've created a "stories" folder in firebase storage under which there is a folder with the "userID" and inside userID, I've stored all the stories of the respective user.

Now, I'm trying to fetch all those stories of the respective user. Here, is my function which I used to fetch the images but it gives me error "Image failed to load"

  String storyUrl = " ";
  void fetchstory() async {
  final ref = FirebaseStorage.instance.ref().child('stories');
    ref.listAll().then((value1) {
      value1.items.forEach((folder) {
        folder.listAll().then((value2) {
          value2.items.forEach((stories) {
            stories.getDownloadURL().then((value) {
              storyUrl = value;
            });
          });
        });
      });
    });
  }

Try to add await and setState like this

await stories.getDownloadURL.then((value){setState({storyUrl = value;});});

Make sure to change stories data type to list because you wanna fetch all data not one story, like this

list<String> stories = "";
await stories.getDownloadURL.then((value){setState({storyUrl.add(value);});});

The value should be a list of string since you are looping it

List<String> storyUrl = [];
  void fetchstory() async {
  final ref = FirebaseStorage.instance.ref().child('stories');
    ref.listAll().then((value1) {
      value1.items.forEach((folder) {
        folder.listAll().then((value2) {
          value2.items.forEach((stories) {
            stories.getDownloadURL().then((value) {
              storyUrl.add(value);//additems like this
              setState((){});
            });
          });
        });
      });
    });
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM