Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undefined class StorageReference when using Firebase Storage

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 {
    // ignore: deprecated_member_use
    var firstImage = await ImagePicker.pickImage(
        source: ImageSource.gallery, imageQuality: 65);
    setState(() {
      _image1 = firstImage;
    });
  }

enter image description here

like image 989
Mt Khalifa Avatar asked Nov 10 '20 06:11

Mt Khalifa


People also ask

How do you use storage reference in flutter?

Create a Reference To download a file, first create a Cloud Storage reference to the file you want to download. You can create a reference by appending child paths to the root of your Cloud Storage bucket, or you can create a reference from an existing gs:// or https:// URL referencing an object in Cloud Storage.

How do I upload images to Firebase storage flutter?

Once you've created an appropriate reference, you then call the putFile() , putString() , or putData() method to upload the file to Cloud Storage. You cannot upload data with a reference to the root of your Cloud Storage bucket. Your reference must point to a child URL.

What is Storage Reference Android?

implements Comparable<StorageReference> Represents a reference to a Google Cloud Storage object. Developers can upload and download objects, get/set object metadata, and delete an object at a specified path. (


Video Answer


2 Answers

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.

like image 125
Peter Haddad Avatar answered Oct 30 '22 02:10

Peter Haddad


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;
}
like image 21
Akash C.A Avatar answered Oct 30 '22 01:10

Akash C.A