5

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.

Mahesh Peri
  • 1,689
  • 2
  • 14
  • 24

3 Answers3

8

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!

Mahesh Peri
  • 1,689
  • 2
  • 14
  • 24
  • 1
    The method 'listAll' isn't defined for the type 'StorageReference'. – jazzbpn Apr 20 '20 at 14:39
  • How you install firebase_storage package in yaml file in flutter ? if you install like this : firebase_storage: ^3.1.5, it will not work as listAll() does not exists. Please check this link : https://github.com/FirebaseExtended/flutterfire/pull/232 and please clearly check my answer. you will get. Flutter team needs to implement listAll(), then only it will work in main package. – Mahesh Peri Apr 22 '20 at 04:38
  • 1
    listAll returns as Future , how could I convert the data to ListResult ? – Memik Jun 10 '20 at 04:01
  • @Memik use then keyword to get futured array. storageRef.listAll().then((result) { print("result is $result"); }); or you can use await - async – Mahesh Peri Jul 01 '20 at 13:00
1

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;

}

Nomixe
  • 61
  • 4
-1

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).