Reputation: 55
I have a problem with my flutter application. I want to upload an image to firebase storage and get the URL but the getDownloadUrl method doesn't work. Here is my code :
import 'package:firebase_storage/firebase_storage.dart' as firebase_storage;
...
Future<String> addImage(File file, firebase_storage.Reference reference) async{
firebase_storage.UploadTask task = reference.putFile(file);
firebase_storage.TaskSnapshot snapshot = task.snapshot;
String urlString = await snapshot.ref.getDownloadURL();
return urlString;
}
I think it's because of the new version of firebase_storage(5.2.0)
Upvotes: 0
Views: 1438
Reputation: 480
You have to wait for the firebase to store the image then only call getUrl by using await. Similarly, check out this link:https://stackoverflow.com/a/52714376/5408464
Future<String> addImage(File file, firebase_storage.Reference reference) async{
final task = await reference.putFile(file);
final urlString = await (await task.onComplete).ref.getDownloadURL().toString();
return urlString;
}
Upvotes: 3
Reputation: 4854
We need to wait for the UploadTask
to complete, as per the source code below:
Future<String> addImage(File file, firebase_storage.Reference reference) async{
firebase_storage.UploadTask task = reference.putFile(file);
String urlString = await (await task.whenComplete).ref.getDownloadURL().toString();
await task.whenComplete(() async {
urlString = await task.ref.getDownloadURL();
}).catchError((onError) {
print(onError);
});
return urlString;
}
Upvotes: -1