I'm trying to list all images in a created folder in FirebaseStorage.
Future getImages(String folderName) async {
final docRef = FirebaseStorage.instance.ref().child(folderName);
List imageRef = [];
docRef.listAll().then((result) async {
imageRef = result.items;
});
return imageRef;
}
FutureBuilder:
FutureBuilder(
future: getImages(images.first), builder: (context, AsyncSnapshot snapshot) { if(snapshot.hasData){ List list = snapshot.data; return Image.network(list.first); } else { return const Center(child: CircularProgressIndicator(),); }
}),
I can not return anything from a then() function neither can I use its value outside its body!
I thought about returning a future object and then use it inside my FutureBuilder but again I need to return a widget and I can't do that inside a then() function
CodePudding user response:
Try to use await
in getImages
to get the list result and return the items like this:
Future<List<Reference>> getImages(String folderName) async {
final docRef = FirebaseStorage.instance.ref().child(folderName);
final listResult = await docRef.listAll();
return Future.value(listResult.items);
}
But if you need the download url of the images, you have to add further code since the above will return a List
of Reference
, and the getDownloadURL
method of Reference
is also async. You could try this if you need a list of urls:
Future<List<String>> getImages(String folderName) async {
final docRef = FirebaseStorage.instance.ref().child(folderName);
final listResult = await docRef.listAll();
final urls = <String>[];
for (var item in listResult.items) {
urls.add(await item.getDownloadURL());
}
return Future.value(urls);
}