Jérémy
Jérémy

Reputation: 81

How to get all files inside a Firebase Storage directory using Flutter

I have an App that display nightclub description and image. Each club have about 4 related image. In Firebase Storage i have created directory for each club and then stored their image inside.

so what i want to do is getting all the image from a club directory so i can display all the image in my app

i think a way of achieving this would be to get the DownloadUrl of each image.

i've tried this :

final StorageReference firebaseStorageRef = FirebaseStorage.instance.ref() .child('profilePics/$clubID/SomeImage.jpg').getDownloadURL();

but since i don't know in advance the name of the image stored i can't use this

so any way of doing this ?

Upvotes: 4

Views: 6413

Answers (4)

Ahmar Tauqir
Ahmar Tauqir

Reputation: 82

Future<void> listExample() async {
    firebase_storage.ListResult result =
        await firebase_storage.FirebaseStorage.instance.ref().listAll();

    result.items.forEach((firebase_storage.Reference ref) {
        print('Found file: $ref');
    });

    result.prefixes.forEach((firebase_storage.Reference ref) {
        print('Found directory: $ref');
    });
}

this code worked for me i got it from flutter fire website

here is the link to the docs https://firebase.flutter.dev/docs/storage/usage

Upvotes: 7

Patrick Schick
Patrick Schick

Reputation: 393

A solution I came up with was, storing a .txt file which contained the name of each file, so first I read the text file, by line-breaking the file apart, and downloading my images in the same folder with the name of the file I got from the text file. This can work, if you can can store the names of all your files in a text file and then upload it!

Flow of the solution:

  • Store all the names of the files in the Firebase Storage's folder in a .txt file
  • write a method to get the text file and the break it line-by-line
  • write a method to get a download url, have this method call each time you get the name of your file from the .txt file.
  • use the image url as per your wish!

Any suggestions or corrections are welcomed!

Upvotes: 1

Praveen Aanand
Praveen Aanand

Reputation: 117

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

Dart file

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

Original post by Mahesh Peri

Upvotes: 0

Doug Stevenson
Doug Stevenson

Reputation: 317467

If you don't know the full path of an object in Cloud Storage, then you can't do anything with it using the mobile client SDKs. Typically, one gets the download URL at the time it was uploaded to the bucket, then writes that URL to a database so it can be queried for later.

Upvotes: 3

Related Questions