首页 > 解决方案 > Flutter - 如何列出 Firebase 存储中的所有图像

问题描述

我需要列出 Firebase 存储中特定目录中的所有图像。此解决方案显示了 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 但它不能识别这个命令。

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

谢谢

标签: flutterfirebase-storage

解决方案


使用此功能列出特定目录中所有照片的 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);
    }
  }

您可以点击此链接了解更多详情。


推荐阅读