如何在 flutter 的 firebase 存储中上传时正确处理任何异常

How to properly handle any exceptions while uploading in firebase storage in flutter

提问人:085Pushpraj Singh Pawar 提问时间:4/6/2023 最后编辑:Frank van Puffelen085Pushpraj Singh Pawar 更新时间:4/6/2023 访问量:53

问:

这是我将 2 个文件上传到 firebase 存储的功能,我不确定我是否使用了正确的异常处理,其次是将 await 语句放在哪里以留出时间上传自己。

void _uploadtocloud() async {
    //File? filesong,fileimage; initialized outside the function
    String songname = songName.text.toString();
    String thumbnail = '${songName.text}thumbnail';

    final Reference storageRef =
        FirebaseStorage.instance.ref().child('music/$songname');
    final UploadTask uploadTaskSong = storageRef.putFile(filesong!);
    final Reference storageRefimage =
        FirebaseStorage.instance.ref().child('image/$thumbnail');
    final UploadTask uploadTaskimage = storageRefimage.putFile(fileimage!);
    final TaskSnapshot snapshot1 = await uploadTaskSong;
    final TaskSnapshot snapshot2 = await uploadTaskimage;

    if (snapshot1.state == TaskState.success &&
        snapshot2.state == TaskState.success) {
      await Future.delayed(const Duration(seconds: 10));
//is this the right place to put await statement.
      setState(() {
        uploaded = true;
        songName.clear();
      });
    } else {
      setState(() {
        uploadfaliure = true;
      });
    }
  }

flutter 异常 调试 firebase-storage

评论

1赞 Harsh Sureja 4/6/2023
请参阅此 firebase.flutter.dev/docs/storage/...
0赞 Rohit Kharche 4/12/2023
这回答了你的问题吗?如何将图片文件上传到 Firebase Storage 并在 Flutter Web 应用中显示

答: 暂无答案