上传多个图像并等待URL列表,图像在几秒钟后成功加载到firestore并返回url,但在此之前,future函数返回null
Future<List<dynamic>> uploadImage() async {
_imageFile.forEach((image) async {
String rannum = Uuid().v1();
StorageReference reference =
FirebaseStorage.instance.ref().child('Reviews').child(rannum);
StorageUploadTask uploadTask = reference.putFile(image);
StorageTaskSnapshot downloadUrl = (await uploadTask.onComplete);
String _url = await downloadUrl.ref.getDownloadURL();
_urllist.add(_url);
});
return _urllist;
}
答案 0 :(得分:0)
您必须这样调用函数:
uploadImage(_imageFile).then((List<String> urls) => urls.forEach((element) => print(element)))
我还更改了您的功能:
Future<List<String>> uploadImage(List<File> _imageFile) async {
List<String> _urllist = [];
await _imageFile.forEach((image) async {
String rannum = Uuid().v1();
StorageReference reference =
FirebaseStorage.instance.ref().child('Reviews').child(rannum);
StorageUploadTask uploadTask = reference.putFile(image);
StorageTaskSnapshot downloadUrl = await uploadTask.onComplete;
String _url = await downloadUrl.ref.getDownloadURL();
_urllist.add(_url);
});
return _urllist;
}
我也不明白为什么你用方括号“ await uploadTask.onComplete”括起来