未处理的异常:PlatformException(download_error,对象不存在于该位置。,null,null),但我的对象确实存在于该位置

Unhandled Exception: PlatformException(download_error, Object does not exist at location., null, null), but my Object does exists at the location

我正在尝试使用以下代码在 firebase 上上传图片:

Future<String> uploadCourseImage(filePath, courseName) async{
  File file = File(filePath);
  var timestamp = Timestamp.now().millisecondsSinceEpoch;
  FirebaseStorage _storage = FirebaseStorage.instance;

  try{
    await _storage.ref().child('courseImage/${this.TeacherName}/$courseName$timestamp').putFile(file);
  }on Exception catch(e){
    print(e.hashCode);
  }

  String downloadURL = await _storage.ref().child('courseImage/${this.TeacherName}/$courseName$timestamp').getDownloadURL();
  this.courseURL =downloadURL;
  notifyListeners();
  return downloadURL;
}

但是我得到这个错误:

Unhandled Exception: PlatformException(download_error, Object does not exist at location., null, null), but my Object does exists at the location

使用 UploadTask class 对象有助于上传我认为的文件。

试试这个方法 上传图片到 Firebase Storage 并在上传完成后提取url。

 Future uploadImage(BuildContext context) async {
    FirebaseStorage storage = FirebaseStorage.instance;
    String? email = FirebaseAuth.instance.currentUser!.email!;
    Reference ref =
        storage.ref().child('users/${email + DateTime.now().toString()}/Profile Image: ${DateTime.now().toString()}');



    UploadTask uploadTask = ref.putFile(_image!);
    final TaskSnapshot downloadUrl = (await uploadTask);
    imageUrl = await downloadUrl.ref.getDownloadURL();

   

 
  
  }

下面的代码对我有用:

 //Create a reference to the location you want to upload to in firebase
StorageReference reference =
    storage.ref().child('courseImage/${this.TeacherName}/$courseName$timestamp');

//Upload the file to firebase
StorageUploadTask uploadTask = reference.putFile(image);

StorageTaskSnapshot taskSnapshot = await uploadTask.onComplete;

// Waits till the file is uploaded then stores the download url
String url = await taskSnapshot.ref.getDownloadURL();