如何在一个查询列表数组中写入多个图像 URL。火力地堡

How can i write multiple images URLs in one list array of query . Flutter Firebase

我想获取 Firebase 存储中的 URL 列表。我想将其添加到我的 Firestore 用户数据中。或者我不知道如何将这些保存在用户数据中以在应用程序中显示 posts 用户个人资料页面。 例如:

  1. 创建一个 post 喜欢的 instagram,

  2. 我输入一些文字说明,

  3. 在图库中选择 3 张图片。

我想将这些图像的 URL 保存在一个文档中,而不是像我的代码当前那样保存在三个单独的文档中。

我的代码是:

   Future uploadInfo() async {
    for (var img in _image) {
      ref = firebase_storage.FirebaseStorage.instance
          .ref()
          .child('images/${Path.basename(img.path)}');
      await ref.putFile(img).whenComplete(() async {
        await ref.getDownloadURL().then((value) {
          posts
              .doc(FirebaseAuth.instance.currentUser!.uid)
              .collection('userPosts')
              .add(
            {
              'imgUrl': value,
            },
          );
        });
      });
    }
}

  1. 您可以使 imageUrlList = imageURL 列表;
  2. 转换为字符串 imageUrlList.toString() 然后将数据保存到一个文档中。
  3. 使用此文档时,只需使用 jsonDecode(documentListOfUrlString) 即可获得图像列表。

您可以有一个包含图像 url 列表的列表,您可以将列表添加到集合中。 见下文:

     Future uploadInfo() async {
        List<String> imageUrlList = []; 
        for (var img in _image) {
          ref = firebase_storage.FirebaseStorage.instance
              .ref()
              .child('images/${Path.basename(img.path)}');
          await ref.putFile(img);
          final String downloadUrl = await ref.getDownloadURL();
          imageUrlList.add(downloadUrl); 
        }
        posts.doc(FirebaseAuth.instance.currentUser!.uid).collection('userPosts')
          .add(
              {
                'imageUrlList': imageUrlList,
              },
          );
        
     }

查看:https://firebase.google.com/docs/firestore/manage-data/add-data