0

I need to list all images from a specific directory in Firebase Storage. This solution shows how listAll() works https://stackoverflow.com/a/58430817/13720706

But if I use this:

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

I get a long string with many values:

result is {prefixes: {}, pageToken: null, items: {2020-06-2816:15:51.669533_250x250.jpg: {bucket: xxxxxxxxxxx.appspot.com, path: /userStorage/xxxxxxxxxxx/k6ouUrjUhMXJjDButEtF/L8H5IVSVhUKPLaGFLBaK/3VfcLmjR8ojxzPCboM9U/thumbs/2020-06-2816:15:51.669533_250x250.jpg, name: 2020-06-2816:15:51.669533_250x250.jpg}, 2020-06-2816:14:31.223608_250x250.jpg: {bucket: xxxxxxxxxxx.appspot.com, path: /userStorage/xxxxxxxxxxx/k6ouUrjUhMXJjDButEtF/L8H5IVSVhUKPLaGFLBaK/3VfcLmjR8ojxzPCboM9U/thumbs/2020-06-2816:14:31.223608_250x250.jpg, name: 2020-06-2816:14:31.223608_250x250.jpg}}}

So the question is how can I tap into this values? How can I get only the path of the images?

I tried this: result.items.path But it does not recognize this command.

Somebody knows how to list the images?

Thanks

ACdev
  • 1

2 Answers2

0

Use this function to list URL of all photos from specific directory.

Future<void> listPhotos() async {
    firebase_storage.ListResult result = await firebase_storage
        .FirebaseStorage.instance
        .ref().child('Gallery').child('Images').listAll();

    for (firebase_storage.Reference ref in result.items) {
     String url = await firebase_storage.FirebaseStorage.instance
      .ref(ref.fullPath)
      .getDownloadURL();
        print(url);
    }
  }

You can follow this link for further details.

Aleem
  • 520
  • 1
  • 4
  • 15
0

You can use this function :

get firebaseStorage => FirebaseStorage.instance;

class StorageMethods extends GetxService {
  @override
  void onInit() {
    super.onInit();
    getBackgrounds();
  }

  List backgrounds = [].obs;
  var loader = false.obs;
  Future<void> getBackgrounds() async {
    loader.value = true;
    ListResult result =
        await firebaseStorage.ref().child('format_backgrounds/').listAll();
    loader.value = false;
    for (Reference ref in result.items) {
      String url = await firebaseStorage.ref(ref.fullPath).getDownloadURL();
      debugPrint("==========>" + url);
      backgrounds.add(url);
    }
  }
}

And then you can use that list containing all your images from firebase storage.

iamSahil
  • 1
  • 2