我对抛出的这个拒绝许可的异常感到非常困惑,但无论我做什么都无法捕获它。
这是我的功能:
Future<bool> uploadDataToStorage(String user, String jsonData) async {
try {
final now = DateTime.now();
final formatter = DateFormat.yMMMMd().add_jm();
final formattedDateTime = formatter.format(now);
final storage = FirebaseStorage.instance;
final Reference storageRef =
storage.ref().child('backup/${user}_backup.json');
FirebaseStorage.instance.setMaxUploadRetryTime(Duration(seconds: 2));
Uint8List data = Uint8List.fromList(utf8.encode(jsonData));
final UploadTask uploadTask = storageRef.putData(data);
return true;
} catch (error) {
print('Data upload error: $error');
return false;
}
}
这就是控制台中显示的内容
E/StorageException(18561): StorageException has occurred.
E/StorageException(18561): User does not have permission to access this object.
E/StorageException(18561): Code: -13021 HttpResult: 403
E/StorageException(18561): The server has terminated the upload session
E/StorageException(18561): java.io.IOException: The server has terminated the upload
session
E/StorageException(18561): at
com.google.firebase.storage.UploadTask.serverStateValid(UploadTask.java:359)
E/StorageException(18561): at
com.google.firebase.storage.UploadTask.shouldContinue(UploadTask.java:325)
....continues..
我只是无法理解这一点。我故意拒绝 Firebase 的权限只是为了处理此错误。
任何帮助将不胜感激。谢谢。
您不会等待
uploadTask
完成,因此您在它有机会成功或失败之前 return true;
。
试试这个
await storageRef.putData(data);
这正是 Firebase 文档中有关上传文件的代码示例所做的,因此我建议将其放在方便的位置。