Undefined class StorageReference when using Firebase Storage

后端 未结 3 1191
甜味超标
甜味超标 2020-12-02 02:43

i am trying to upload image, and the same process is working for my other app, but here it gives these errors, can you guy plz help?

Future getImage1() async          


        
相关标签:
3条回答
  • 2020-12-02 03:14

    Starting from Version firebase_storage 5.0.1:

    You have to do the following:

    FirebaseStorage storage = FirebaseStorage.instance;
    Reference ref = storage.ref().child("image1" + DateTime.now().toString());
    UploadTask uploadTask = ref.putFile(_image1);
    uploadTask.then((res) {
       res.ref.getDownloadURL();
    });
    

    StorageReference class has been removed and now you have to use the class Reference. UploadTask extends Task, which also implements Future<TaskSnapshot>. Therefore all the methods that are in the class Future can be used on the class UploadTask.

    So to get the url of the image, you need to use the then() method which registers a callback to be called when this future completes.

    0 讨论(0)
  • 2020-12-02 03:28

    As mentioned by @PeterHadad there are a few breaking changes in firebase storage 5.0.1. The classes have been renamed but maintain most of their old functionalities.

    You can also use .whenComplete() to get the download URL as follows-

    uploadPic(File _image1) async {
       FirebaseStorage storage = FirebaseStorage.instance;
       String url;
       Reference ref = storage.ref().child("image1" + DateTime.now().toString());
       UploadTask uploadTask = ref.putFile(_image1);
       uploadTask.whenComplete(() {
          url = ref.getDownloadURL();
       }).catchError((onError) {
        print(onError);
        });
       return url;
    }
    
    0 讨论(0)
  • 2020-12-02 03:31

    Looks like you haven't added dependency in pubspec.yaml file or maybe you didn't imported package in your dart file. That's why you're getting error.

    Check the package site for example on how to use: https://pub.dev/packages/firebase_storage

    0 讨论(0)
提交回复
热议问题