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.
Related
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.
so i've tried to upload an image to firestore, but it gives me the error Unsupported operation: _Namespace.
Here's my method:
File pickedImage;
Future<Uri> uploadPic() async {
FirebaseStorage _storage = FirebaseStorage.instance;
StorageReference reference = _storage.ref().child("profile_pictures/");
StorageUploadTask uploadTask = reference.putFile(pickedImage);
Uri dowurl = await (await uploadTask.onComplete).ref.getDownloadURL();
return dowurl;
}
Try adding a name to the file you want to save:
StorageReference reference = _storage.ref().child("profile_pictures/ –add Name here –");
You might also remove the / that comes after profile_pictures, if you don‘t want to add another reference
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"
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
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();