Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter Load Image from Firebase Storage

Tags:

flutter

dart

I see there are a lot of examples on how to upload an image using flutter to firebase storage but nothing on actually downloading/reading/displaying one that's already been uploaded.

In Android, I simply used Glide to display the images, how do I do so in Flutter? Do I use the NetworkImage class and if so, how do I first get the url of the image stored in Storage?

like image 687
spongyboss Avatar asked Jun 15 '18 14:06

spongyboss


People also ask

How do I get pictures from Firebase storage flutter?

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.

How do you show pictures from storage on flutter?

File and FileImage . Sometimes an application needs to be able to display images from files. In Flutter, displaying an image can be done by using Image widget. Flutter provides a named constructor File.


1 Answers

Firebase Console

To view the images inside your storage, what you need is the name of the file in the storage. Once you've the file name for the specific image you need. In my case if i want the testimage to be loaded,

final ref = FirebaseStorage.instance.ref().child('testimage'); // no need of the file extension, the name will do fine. var url = await ref.getDownloadURL(); print(url); 

Once you've the url,

Image.network(url); 

That's all :)

New alternative answer based on one of the comments.

I don't see anywhere google is charging extra money for downloadURL. So if you're posting some comments please attach a link to it.

Once you upload a file to storage, make that filename unique and save that name somewhere in firestore, or realtime database.

getAvatarUrlForProfile(String imageFileName) async { final FirebaseStorage firebaseStorage = FirebaseStorage(     app: Firestore.instance.app,     storageBucket: 'gs://your-firebase-app-url.com');  Uint8List imageBytes; await firebaseStorage     .ref()     .child(imageFileName)     .getData(100000000)     .then((value) => {imageBytes = value})     .catchError((error) => {}); return imageBytes; }  Uint8List avatarBytes =     await FirebaseServices().getAvatarUrlForProfile(userId); 

and use it like,

MemoryImage(avatarBytes) 
like image 136
Manoj Perumarath Avatar answered Sep 23 '22 08:09

Manoj Perumarath