使用onComplete函数将图像添加到Firebase Storage的方法是什么?



我制作了一个函数来上传三个图像到firebase存储。我一直在一个教程的帮助下做这件事。由于教程使用旧版本代码,我正在尽最大努力将其更改为更新版本。在本教程中,它使用了onComplete方法获取任务快照。但是在pub.dev文档中,据说这些方法已经被删除,并且它们是一个异常,像下面这样(break: iscancelled, isComplete, isInProgress, isPaused和isSuccessful现在已被删除。相反,你应该订阅流(用于paused/progress/complete/error事件)或订阅任务Future(用于任务完成/错误)。

)请告诉这是什么意思,我怎么能改变我下面的代码根据它。在我输入onComplete的地方出现错误

void validateAndUpload() async{
if (_formKey.currentState.validate()) {
if (_image1 != null && _image2 != null && _image3 != null) {
if (selectedSizes.isNotEmpty) {
String imageUrl1;
String imageUrl2;
String imageUrl3;
final FirebaseStorage storage = FirebaseStorage.instance;
final String picture1 =
'1${DateTime.now().millisecondsSinceEpoch.toString()}.jpg';
UploadTask task1 = storage.ref().child(picture1).putFile(_image1);
final String picture2 =
'2${DateTime.now().millisecondsSinceEpoch.toString()}.jpg';
UploadTask task2 = storage.ref().child(picture2).putFile(_image2);
final String picture3 =
'3${DateTime.now().millisecondsSinceEpoch.toString()}.jpg';
UploadTask task3 = storage.ref().child(picture3).putFile(_image3);
TaskSnapshot snapshot1 = await task1.onComplete.then((snapshot) => snapshot);
TaskSnapshot snapshot2 = await task1.onComplete.then((snapshot) => snapshot);
task3.onComplete.then((snapshot3) async{
imageUrl1 = await snapshot1.ref.getDownloadURL();
imageUrl2 = await snapshot2.ref.getDownloadURL();
imageUrl3 = await snapshot3.ref.getDownloadURL();
});

} else {
Fluttertoast.showToast(
msg: "Sizes cannot be Empty",
backgroundColor: Colors.red,
textColor: Colors.white);
}
} else {
Fluttertoast.showToast(
msg: "Images are not Filled",
backgroundColor: Colors.red,
textColor: Colors.white);
}
}
}

你就快成功了!正如错误所说,要么您必须侦听UploadTask流,要么等待UploadTask完成。对于您的情况,

TaskSnapshot snapshot1 = await task1;
TaskSnapshot snapshot2 = await task2;
TaskSnapshot snapshot3 = await task3;
imageUrl1 = await snapshot1.ref.getDownloadURL();
imageUrl2 = await snapshot2.ref.getDownloadURL();
imageUrl3 = await snapshot3.ref.getDownloadURL();

更多参考- https://firebase.flutter.dev/docs/storage/usage/#handling-tasks

相关内容

最新更新