获取Firebase存储文件夹中所有图片的URL。

huangapple go评论63阅读模式
英文:

Flutter : get url of all image in firebase storage folder

问题

以下是要翻译的部分:

当前情况

我在 Firebase 存储中有一个文件夹,其中包含我想在我的 Flutter 应用程序中使用的多张图片。我想要检索文件夹中所有图片的 URL,以便在 CircleAvatar 的 backgroundimage 中使用它。为了获取这些 URL,我正在使用两个函数。

getIconUrl(String name) async {
  final ref = storageRef.child(name);
  final url = await ref.getDownloadURL();
  return url;
}

iconUrlFill(Map dict) async {
  Map iconResult = {};
  for (String key in dict.keys) {
    var urlResult = getIconUrl(key);
    iconResult[key] = urlResult;
  }
  return iconResult;
}

并尝试在 FutureBuilder 中使用它。

问题

上面的代码生成了一个 Map,其中键为 String,值为 Future<String>,我假设 Future<String> 包含了 URL。当我在 FutureBuilder 中使用 getIconUrl 时,我可以通过 snapshot.data! 访问 URL,但我不知道如何在 iconUrlFill 函数中做到这一点。

我寻求的解决方案

我寻求以下问题的解决方案:

  1. 如何在 iconUrlFill 函数的循环中访问由 getIconUrl 检索到的 Future<String> 内部的 URL?
  2. 有没有一种方法可以获取 Firebase 存储文件夹中所有图片的 URL?
英文:

Current Situation

I have folder in firebase storage that contain number of image I want to use in my flutter app.
I would like to retrive url for all the image in the folder to use it for networkimage in backgroundimage in CircleAvatar.
To fetch the url I'm using two fuction.

getIconUrl(String name) async {
  final ref = storageRef.child(name);
  final url = await ref.getDownloadURL();
  return url;
}

iconUrlFill(Map dict) async {
  Map iconResult = {};
  for (String key in dict.keys) {
    var urlResult = getIconUrl(key);
    iconResult[key] = urlResult;
  }
  return iconResult;
}

and trying to use it in futurebuilder.

Problem

Code above result in Map that key of String and value of Future<String>, which I assume Future<String> are url. When I use getIconUrl for futureBuilder I can reach to url by snapshot.data! but I don't know how to do that in iconUrlFill function.

Solution I seek for

I'm looking for solution to either question

  1. how to reach to url inside Future<String>, retrieved by getIconUrl, inside loop f iconUrlFill function?
  2. is there way to gain all url of image in firebase storage folder?

答案1

得分: 1

因为你正在尝试从getIconUrl获取未来的数据,所以你需要在该函数上使用await来获取结果。

所以你的代码可能需要像这样:

Future getIconUrl(String name) async {
  final ref = storageRef.child(name);
  final url = await ref.getDownloadURL();
  return url;
}

iconUrlFill(Map dict) async {
  Map iconResult = {};
  for (String key in dict.keys) {
    var urlResult = await getIconUrl(key);
    iconResult[key] = urlResult;
  }
  return iconResult;
}
希望这有所帮助。
如果我有任何误解,请务必告诉我。
英文:

Because you're trying to get the result from getIconUrl which is future data you'll need to await on that function

So here's how your code might need to look like

Future getIconUrl(String name) async {
  final ref = storageRef.child(name);
  final url = await ref.getDownloadURL();
  return url;
}

iconUrlFill(Map dict) async {
  Map iconResult = {};
  for (String key in dict.keys) {
    var urlResult = await getIconUrl(key);
    iconResult[key] = urlResult;
  }
  return iconResult;
}

hope it helped.
And obviously, please let me know if i misunderstood any part.

huangapple
  • 本文由 发表于 2023年7月18日 16:08:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76710710.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定