【问题标题】:flutter firebase firestore: How to get images stored in array firestore?flutter firebase firestore:如何获取存储在数组firestore中的图像?
【发布时间】:2021-07-23 02:03:27
【问题描述】:

我想通过存储在 Firebase firestore 中的 url 显示带有索引的图像,我正在获取存储在云存储中的图像,但我不知道如何获取存储在数组中的 firebase firestore 中的 url。请帮助我,因为我只是一个初学者并通过互联网学习。图片附在下面。 提前致谢。

List<String> urls = [];

  @override
  void initState() {
  super.initState();

 _getImages();
 
}

这是通过存储在云存储中的 url 显示图像的容器。

                Container(

                  height: 160.0,
                  padding: EdgeInsets.symmetric(vertical: 15.0,horizontal: 15.0),

                    child: CustomScrollView(
                      
                      physics: const BouncingScrollPhysics(),


                      slivers: <Widget>[
                        SliverPadding(

                          padding: const EdgeInsets.all(10),
                          sliver:
                          _buildContent(urls),

                        )
                      ],
                    ),
                ),

这是我将图像存储在云存储中的方法。

_getImages() async {

ListResult _result = await _stoarge.ref().child("images").list(ListOptions(maxResults: 10));
_result.items.forEach((_ref) async {
  String _u = await _ref.getDownloadURL();

  urls.add(_u);
  setState(() {
    print(urls.length);
    print(urls);
  });
});

 }

【问题讨论】:

    标签: firebase flutter google-cloud-firestore


    【解决方案1】:

    把它放在你的getImages()函数中

    //First, you must retrieve your document, like this:
    DocumentSnapshot firebaseDoc = await FirebaseFirestore.instance.collection('users').doc('yourDocumentId').get();
    
    //then you need to extract your list, which is in the 'images' field, you can do so like this:
    List<String> imagesList = firebaseDoc.data()['images'];
    
    //Now, you have a list, containing the URLs you want.
    print(imagesList[0]); //should print the first URL, you can take it from here
    

    【讨论】:

    • 感谢您的回复,但现在我收到此错误。 " 未处理的异常:类型 'List' 不是类型 'List' 的子类型"
    • 改成这样:List imagesList = firebaseDoc.data()['images'];
    • 还是同样的错误——[ERROR:flutter/lib/ui/ui_dart_state.cc(186)] 未处理的异常:类型 'List' 不是类型 'List 的子类型'
    • 很抱歉打扰您的新查询,现在它正在控制台中打印所有 url 但没有索引,我当时需要删除图像,但它仍然没有显示在容器中,如何让它们显示并带有索引?
    • 你不需要索引,你可以删除你想要的元素,而不必传递索引。
    猜你喜欢
    • 2020-03-28
    • 2021-10-14
    • 2020-11-08
    • 2021-02-27
    • 2020-09-24
    • 1970-01-01
    • 1970-01-01
    • 2022-01-07
    • 1970-01-01
    相关资源
    最近更新 更多