How to get full downloadUrl from UploadTaskSnapshot in Flutter? - firebase

I correctly receive UploadTaskSnapshot, and the field downloadUrl contains an instance of Uri that parses download link of uploaded file.
How to get storage and downloadUrl as strings?

old
final uploadTask = imageStore.putFile(imageFile);
final url = (await uploadTask.future).downloadUrl;
update
This answer https://stackoverflow.com/a/52690889/217408 is now the accurate one.
final ref = FirebaseStorage.instance
.ref()
.child('path')
.child('to')
.child('the')
.child('image_filejpg');
ref.putFile(imageFile);
// or ref.putData(Uint8List.fromList(imageData));
var url = await ref.getDownloadURL() as String;
or
var url = Uri.parse(await ref.getDownloadURL() as String);

I get downloadUrl from v1.0.3 by the following code.
StorageReference storageReference = _storage.ref().child(path);
StorageUploadTask uploadTask = storageReference.putFile(imageFile);
StorageTaskSnapshot taskSnapshot = await uploadTask.onComplete;
String downloadUrl = await taskSnapshot.ref.getDownloadURL();

#DomingoMG it looks like with the latest release they want:
String location = await ref.getDownloadURL();
See https://pub.dartlang.org/packages/firebase_storage#-example-tab-

Update: Nov 2020
onComplete is now removed from the upload task. So, use:
var reference = FirebaseStorage.instance.ref().child("your_path");
await reference.putFile(fileToUpload);
var url = await reference.getDownloadURL();

Related

Undefined class 'StorageTaskSnapshot'. Try changing the name to the name of an existing class or creating a class with the name 'StorageTaskSnapshot'

I want to upload a photo to the firebase. When I tried to use StorageTaskSnapshot it makes underline StorageTaskSnapshot saying Undefined class 'StorageTaskSnapshot'.
This is my code:
Future<String> uploadPhoto(img) async {
UploadTask task =
FirebaseStorage.instance.ref().child("Posts Pictures").child("post_$postId.jpg").putFile(img);
StorageTaskSnapshot storageTaskSnapshot =await task.onComplete;
String downloadUrl = await storageTaskSnapshot.ref.getDownloadURL();
return downloadUrl;
}
Anyone Please Help me.
Try the following:
Reference ref = FirebaseStorage.instance.ref();
TaskSnapshot uploadFile = await child("Posts Pictures").child("post_$postId.jpg").putFile(img);
if (uploadFile.state == TaskState.success) {
final String downloadUrl = await snapshot.ref.getDownloadURL();
}
First get the reference to the firebase storage instance, then using putFile() you can upload the file to the storage which also returns a TaskSnapshot. After that if the state of the uploaded file is success then get the downloaded url.

Flutter: How to get multi download url?

Future<List<dynamic>> uploadImage({List<File> images, String Key}) async {
List<dynamic> imagesUrls = [];
images.asMap().forEach((index, image) async {
final StorageReference storageReference = FirebaseStorage().ref().child(_getImagePathByHereKey(Key, index: index));
final StorageUploadTask uploadTask = storageReference.putFile(image);
});
return imagesUrls;
}
I want to get download url and put it to imagesUrls. But imagesUrls is empty. How to get download url?
https://pub.dev/documentation/firebase/latest/firebase/StorageReference-class.html
There's a method called getDownloadUrl() which returns a Future. I'm assuming that's what you're looking for. I think you use it like this:
List<dynamic> imagesUrls = images.asMap().entries.map((entry) async {
final StorageReference storageReference = FirebaseStorage().ref().child(_getImagePathByHereKey(Key, index: entry.key));
final StorageUploadTask uploadTask = storageReference.putFile(entry.value);
return await storageReference.getDownloadUrl();
}).toList();
return imagesUrls;
I think entries.map can't take an async method though so you might wanna use streams. Think you got it from here though.

Flutter Firebase upload multiple images

Uploading multiple images and waiting for the URL list, images were loaded to firestore successfully after few seconds and returns the url but before that the future function returns with null
Future<List<dynamic>> uploadImage() async {
_imageFile.forEach((image) async {
String rannum = Uuid().v1();
StorageReference reference =
FirebaseStorage.instance.ref().child('Reviews').child(rannum);
StorageUploadTask uploadTask = reference.putFile(image);
StorageTaskSnapshot downloadUrl = (await uploadTask.onComplete);
String _url = await downloadUrl.ref.getDownloadURL();
_urllist.add(_url);
});
return _urllist;
}
You have to call your function like this:
uploadImage(_imageFile).then((List<String> urls) => urls.forEach((element) => print(element)))
And I also changed your function:
Future<List<String>> uploadImage(List<File> _imageFile) async {
List<String> _urllist = [];
await _imageFile.forEach((image) async {
String rannum = Uuid().v1();
StorageReference reference =
FirebaseStorage.instance.ref().child('Reviews').child(rannum);
StorageUploadTask uploadTask = reference.putFile(image);
StorageTaskSnapshot downloadUrl = await uploadTask.onComplete;
String _url = await downloadUrl.ref.getDownloadURL();
_urllist.add(_url);
});
return _urllist;
}
also I don't understand why you enclosed with brackets "await uploadTask.onComplete"

How to get the download URL from firebase with flutter?

I would like to retrieve the Download URL of a private image in firbase storage.
Hi have tried many of the suggestions on the site, but all of them end up in the same result.
I have tried the following code:
getImageNow() async {
StorageReference ref =
FirebaseStorage.instance.ref().child("/1.jpg");
String url = (await ref.getDownloadURL()).toString();
return url;
}
It works when i print the url inside the function, but when i try to call print(getImageNow())to get the url, i just get "Instance of 'Future<dynamic>'"
UPDATE*************
In the end i am trying to get somthing like this:
return Image.network(
getImageNow(),
);
But i can not get it to work with async.
Since getImageNow() is asynchronous (as indicated by the async keyword), you will need to use await to make the calling code wait for the result:
print(await getImageNow())
What await does here is that it essentially unwraps the Future and is equivalent to:
getImageNow().then((value) => print(value));
this is how i upload and get the download url
this part is how i get image from picker
Future getImage() async {
var image = await ImagePicker.pickImage(source: ImageSource.gallery);
setState(() {
_image = image;
print('Image Path $_image');
});
}
then i upload it
Future uploadPic(BuildContext context) async {
String fileName = basename(_image.path);
StorageReference firebaseStorageRef = FirebaseStorage.instance.ref().child(fileName);
StorageUploadTask uploadTask = firebaseStorageRef.putFile(_image);
StorageTaskSnapshot taskSnapshot = await uploadTask.onComplete;
final String url = (await taskSnapshot.ref.getDownloadURL());
print('URL Is $url');
}
hope it will help someone

How to Load Image to Firebase Storage and get downloadUrl (Flutter/Firebase)

I want to implement sending files to Firebase Storage, as well as get a link to it. But for some reason it does not work ...
void _pickFile() async {
File file = await FilePicker.getFile(type: FileType.ANY);
StorageReference ref = FirebaseStorage.instance.ref()
.child("image${Random().nextInt(999)}.jpg");
var fileUrl = ref.putFile(file).onComplete.then((file) =>
file.ref.getDownloadURL());
print(fileUrl);
_sendMessage(fileUrl: fileUrl.toString());
}
...
prefixIcon: IconButton(
icon: Icon(Icons.attach_file),
color: Colors.white,
onPressed: () => _pickFile()
)
Why do I get this instead of a link?
I/flutter (16610): Instance of 'Future'
I need a link in the String!
What is the problem?
Like the log say, fileUrl is a Future who is not resolved yet. You should make sure it's resolve to make use of the string give.
void _pickFile() async {
File file = await FilePicker.getFile(type: FileType.ANY);
StorageReference ref = FirebaseStorage.instance.ref()
.child("image${Random().nextInt(999)}.jpg");
String fileUrl = await (await ref.putFile(file).onComplete).ref.getDownloadURL();
print(fileUrl);
_sendMessage(fileUrl: fileUrl.toString());
}
You need use await for Future functions
void _pickFile() async {
File file = await FilePicker.getFile(type: FileType.ANY);
StorageReference ref = FirebaseStorage.instance.ref()
.child("image${Random().nextInt(999)}.jpg");
final StorageUploadTask uploadTask = ref.putFile(file);
StorageTaskSnapshot taskSnapshot = await uploadTask.onComplete;
String downloadUrl = await taskSnapshot.ref.getDownloadURL();
_sendMessage(fileUrl: downloadUrl);
}
It would look like this without using await
void _pickFile() async {
File file = await FilePicker.getFile(type: FileType.ANY);
StorageReference ref = FirebaseStorage.instance.ref()
.child("image${Random().nextInt(999)}.jpg");
ref.putFile(file).onComplete.then((file){
var fileUrl = file.ref.getDownloadURL());
_sendMessage(fileUrl: fileUrl.toString());
});
}
here is my solution to upload and get the uploaded image url
first i get image with image picker
Future getImage() async {
var image = await ImagePicker.pickImage(source: ImageSource.gallery);
setState(() {
_image = image;
print('Image Path $_image');
});
}
then i upload to firebase
Future uploadPic(BuildContext context) async {
String fileName = basename(_image.path);
StorageReference firebaseStorageRef = FirebaseStorage.instance.ref().child(fileName);
StorageUploadTask uploadTask = firebaseStorageRef.putFile(_image);
StorageTaskSnapshot taskSnapshot = await uploadTask.onComplete;
final String url = (await taskSnapshot.ref.getDownloadURL());
print('URL Is $url');
setState(() {
print("Profile Picture uploaded");
print("....................$url");
Scaffold.of(context).showSnackBar(
SnackBar(content: Text('Profile Picture Uploaded')));
});
}
this is the part i get the downloadUrl of currently uploaded image
final String url = (await taskSnapshot.ref.getDownloadURL());
print('URL Is $url');

Resources