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;
});
}
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.
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.
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. (
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.
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With