Undefined class StorageReference when using Firebase Storage
Asked Answered
V

5

20

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

Vasos answered 10/11, 2020 at 6:50 Comment(2)
did you add firebase_storage?Loadstone
yes, I did @PeterHaddadVasos
L
67

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.

Loadstone answered 10/11, 2020 at 7:16 Comment(2)
How do you return the url from this?Keller
@Keller if you want to return the url, then create a method of type Future<String> and use async/await instead of thenLoadstone
A
10

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;
}
Analysand answered 10/11, 2020 at 9:0 Comment(1)
uploadTask.whenComplete(() async { url = await ref.getDownloadURL(); }).catchError((onError) { print(onError); });Hoofbound
D
1

Pick image from gallery and store it firebase storage in images folder like this :

final XFile? image = await ImagePicker().pickImage(source: source);
FirebaseStorage storage = FirebaseStorage.instance;
Reference ref = storage.ref().child("images/"+DateTime.now().toString());
UploadTask uploadTask = ref.putFile(File(image!.path));
uploadTask.then((res) {
  res.ref.getDownloadURL();
});

Make sure to add image_picker and firebase_storage in your pubspec.yaml file as dependencies.

Document answered 12/12, 2021 at 17:23 Comment(0)
T
0

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

Tina answered 10/11, 2020 at 8:56 Comment(0)
V
0

If you want a function that returns the url you can use

Future<String> uploadPicture(XFile image, Reference ref) async {
    File imageFileToSave = File(image.path);

    UploadTask uploadTask = ref.putFile(imageFileToSave);
    String url = await uploadTask.then((res) {
      return res.ref.getDownloadURL();
    });
    return url;
  }

Now to retrieve the url by calling this function simply do:

String imageUrl = await uploadPicture(newImage, ref);
Vicennial answered 22/3, 2023 at 18:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.