Flutter - Get all images from firebase storage

Viewed 6116

I am manually upload my images to firebase storage console in web. I want to download all images to my flutter android app. But it is getting only one image at a time with getDownloadUrl() method.

In android, listAll() method List all items (files) and prefixes (folders) under this StorageReference.

Like this, anyone suggest me in flutter.

I saw stack over flow answers like there is no api for download all images at once. But any suggestion/ideas would be great.

3 Answers

Finally got the solution.

Flutter package firebase_storage: ^3.0.6 has no method called listAll(). Only able to get single file/image download url using getDownloadURL() method from firebase storage.

Recently (19 hours ago, 16th Oct 2019) flutter team has added this functionality to get all files and folders using listAll() method. Below is the git link.

https://github.com/FirebaseExtended/flutterfire/pull/232

Need to use package in pubspec.yaml like below :

firebase_storage:
    git:
      url: git://github.com/danysz/flutterfire.git
      ref: master
      path: packages/firebase_storage

This is temporary solution until they update this package version firebase_storage: ^3.0.6

Example Code : 

void getFirebaseImageFolder() {
    final StorageReference storageRef =
        FirebaseStorage.instance.ref().child('Gallery').child('Images');
    storageRef.listAll().then((result) {
      print("result is $result");
    });
  }

Hope it will be useful for many people. Happy coding!

in my cause this code is work me.. for every unique user and get list of images.. you can specify your reference path then get list of images Tested image

  static Future<List<Map<String, dynamic>>> fetchImages(
  String uniqueUserId) async {
List<Map<String, dynamic>> files = [];
final ListResult result = await FirebaseStorage.instance
    .ref()
    .child('Gallery')
    .child('images')
    .child(uniqueUserId)
    .list();
final List<Reference> allFiles = result.items;
print(allFiles.length);

await Future.forEach<Reference>(allFiles, (file) async {
  final String fileUrl = await file.getDownloadURL();
  final FullMetadata fileMeta = await file.getMetadata();
  print('result is $fileUrl');

  files.add({
    'url': fileUrl,
    'path': file.fullPath,
    'uploaded_by': fileMeta.customMetadata['uploaded_by'] ?? 'Nobody',
    'description':
        fileMeta.customMetadata['description'] ?? 'No description'
  });
});

return files;

}

if only need specific images you can use the Image.network(url) method for retrieving the image. only thing you have to done is you have to copy the access token related to your specific image and paste it inside the Image.newtork(url). remember to cover the url with '' . otherwise you can use the method below

Future downloadImage()async{
    StorageReference _reference=FirebaseStorage.instance.ref().child("images/$_imageName.png");
    String downloadAddress=await _reference.getDownloadURL();
    setState(() {
      _downloadUrl=downloadAddress;
    });
  }

now you can use the retrieve the image using Image.network(_downloadUrl).

Related