简体   繁体   中英

How to get a Stream of download url in firebase flutter

i want to create a stream of firebase storage download link for all the images

   firebase_storage.FirebaseStorage storage =
       firebase_storage.FirebaseStorage.instance;
   firebase_storage.Reference ref =
       storage.ref().child('images');
   firebase_storage.ListResult result = await ref.listAll();
   result.items.forEach((firebase_storage.Reference ref) async* {
     print('Found file: $ref');
     yield (await ref.getDownloadURL()).toString();
   });
 }

the snapshot data is null if i create with future it seems to work when i add all the urls in a list and give to listview builder but i cant get each string through stream

I've had problems with ForEach and await getDownloadUrl(). Try using a for loop instead.

 for (Reference refin result.items) async* {
  print('Found file: $ref');
  yield (await getDownloadUrl().toString());
 }

First thing first create a instance of FirebaseStorage

final _firestorage = FirebaseStorage.instance;

after then we need ref. for get image from storage.

final ref = _firestorage.ref().child('image');

and then we can create image's url.

var imageURL = await ref.getDownloadURL();

and try this.

print(imageURL);

if you wanna get this codes into fully method:

String getImageURL(){
  final _firestorage = FirebaseStorage.instance;
  final ref = _firestorage.ref().child('image');
  var imageURL = await ref.getDownloadURL();
  if(imageURL != null){
    return imageURL;
  }else{
    print("Couldn't load image");
  }
}

Also you can get this image with StreamBuilder or FutureBuilder etc. for get more about FirebaseStorage check it out this documentation

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