【发布时间】:2021-10-14 08:44:31
【问题描述】:
我正在尝试将多张图片上传到 Firebase 存储。我遇到的问题是尝试检索每个图像的 URL。如果我不尝试检索 URL,文件本身会成功上传,我看到的问题是上传与生成的 url 并行运行,因此在到达第二张图像时没有更新 url。
我得到的错误是“[firebase_storage/object-not-found] 在所需的引用处不存在对象。”
注意:我没有使用 forEach,因为我将要上传的文件数限制为 5。下面的代码包含在 if 语句中以检查列表长度。
for (var i = 0; i < _imageFileList!.length; i++) {
if (kIsWeb) {
var bytes = await _imageFileList![i].readAsBytes();
print('photo$i');
Reference snapshot = await FirebaseStorage.instance.ref().child('items/$itemkey/photo$i');
UploadTask uploadFile = snapshot.putData(bytes);
// If I delete the following 2 lines then the uploadof multiple files works without any issues.
imagesUrls.add(await (snapshot.getDownloadURL()));
print(imagesUrls);
///// ///// ///// ///// ///// /////
} else {
String filePath = 'items/${itemkey}/photo${i}.jpg';
Reference snapshot = FirebaseStorage.instance.ref().child(filePath);
UploadTask uploadFile = snapshot.putFile(File(_imageFileList![i].path));
// currently doesnt work but testing on web so this doesnt trigger.
final String downloadUrl = await snapshot.getDownloadURL();
print(downloadUrl);
//////////////////////////////////
}
}
如何让这些运行异步或等待?我不能将 await 放在 for 循环前面,因为颤动告诉我我不能将 await 用于正常的 for 循环。
【问题讨论】:
标签: flutter for-loop firebase-storage