Flutter-如何列出Firebase存储中的所有图像

时间:2020-07-09 14:55:14

标签: flutter firebase-storage

我需要列出Firebase Storage中特定目录中的所有图像。 此解决方案显示listAll()如何工作https://stackoverflow.com/a/58430817/13720706

但是如果我使用这个:

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

我得到一个包含许多值的长字符串:

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}}}

所以问题是我如何利用这个值?如何只获取图像的路径?

我尝试过: result.items.path 但是它无法识别此命令。

有人知道如何列出图像吗?

谢谢

1 个答案:

答案 0 :(得分:0)

使用此功能可以列出特定目录中所有照片的 URL。

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);
    }
  }

您可以关注this link了解更多详情。