gpt4 book ai didi

firebase - 我如何上传多张图片到 flutter 中的 firebase 并获取它们的所有下载 url

转载 作者:行者123 更新时间:2023-12-05 08:14:17 25 4
gpt4 key购买 nike

这是我的上传功能。我想要的输出是所有 url 的列表,但它返回一个空列表。我尝试了不同的建议解决方案,但都失败了。

Future<List<String>> uploadFiles(List _images) async {
List<String> imagesUrls=[];

_images.forEach((_image) async{
StorageReference storageReference = FirebaseStorage.instance
.ref()
.child('posts/${_image.path}');
StorageUploadTask uploadTask = storageReference.putFile(_image);
await uploadTask.onComplete;

imagesUrls.add(await storageReference.getDownloadURL());

});
print(imagesUrls);
return imagesUrls;
}

最佳答案

我认为您需要 Future.wait 以确保在继续之前解决所有 futures:

Future<List<String>> uploadFiles(List<File> _images) async {
var imageUrls = await Future.wait(_images.map((_image) => uploadFile(_image)));
print(imageUrls);
return imageUrls;
}

Future<String> uploadFile(File _image) async {
StorageReference storageReference = FirebaseStorage.instance
.ref()
.child('posts/${_image.path}');
StorageUploadTask uploadTask = storageReference.putFile(_image);
await uploadTask.onComplete;

return await storageReference.getDownloadURL();
}

关于firebase - 我如何上传多张图片到 flutter 中的 firebase 并获取它们的所有下载 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63513002/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com