如何修复itemCount以从Firestore获取数据到列表视图

问题描述 投票:0回答:1

我有Listview.builder,其中包含来自Firestore中的数组的数据,该数据属于子集合中的数组我的结构数据,像这样但是,当要显示该数组到Listview时,它只显示第一个,即0。我需要将数组中的所有数据都获取到列表视图。如何修复itemCount以获取所有数据?

Collcetion 
-Institute
--document
---PI3naj7N7GgwlDSjK3rV0Twiu2S2
----subCollcetion 
------Ravs 
-------documentsubCollcetion 
English 
field
Ravs name :English 

item array 
0 
Contains Data Map 
1
Contains Data Map
2
Contains Data Map
.
.
.
.

此功能

 StreamBuilder(
          stream:Firestore.instance.collection("Institute").document(
              'PI3naj7N7GgwlDSjK3rV0Twiu2S2').collection("Ravs")
              .where('Ravs name',isEqualTo:'English ' ).snapshots() ,
          builder: (context, AsyncSnapshot<QuerySnapshot> snapshot) {

            if (snapshot.hasError) return Container(
              alignment: Alignment.center,
              child: Text(
                'it has error ',
                style: TextStyle(fontSize: 12),),


            );
            return snapshot.hasData

                ? Scaffold(
              appBar: new AppBar(backgroundColor: Colors.brown[600],),
              body:Card(
             child:ListView.builder(
              itemCount:snapshot.data.documents.length,

              itemBuilder: (_,index){
                  DocumentSnapshot ds =snapshot.data.documents[index];
                return Directionality(
                   textDirection: TextDirection.rtl,
                   child:Card(
                    child:InkWell(
                    onTap: (){

                },
                   child:Column(
                      children: <Widget>[
                         new Container(
                          padding: const EdgeInsets.only(top: 10),
                           width: 500,
                          decoration: BoxDecoration(
                            borderRadius: BorderRadius.all(Radius.circular(150)),

                          ),
                         alignment: Alignment.topCenter,
                        child:
 FadeInImage.assetNetwork(fit: 
BoxFit.fill,placeholder:
 'assets/images/image.jpg', 
image: ds.data['item'][index]['Iamge path']),

                         ),

                        new Container(
                            padding: const EdgeInsets.all(10.0),
                            alignment: Alignment.topRight,
                            child: Column(
                              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: <Widget>[
                                Text(" name Rav:${ds.data['item'][index]['Ravs_name'] }",textDirection: TextDirection.rtl, style: Theme.of(context).textTheme.title),
                                Text("Date:${ds.data['item'][index]['Rav_Date']} ",textAlign: TextAlign.right, style: TextStyle(color: Colors.black.withOpacity(0.5))),
                              ],
                            )
                        )    
                         ],

                   )
                )
                   )
                );

              }

            ),
              )
            ) : Center(child: CircularProgressIndicator(),);
          },

        );
flutter google-cloud-firestore
1个回答
0
投票

您要呈现的此结构似乎无法在集合中进行收集。但是,由于可以运行您的代码,因此我假设在结构Ravs中是具有字段Rav name的文档的子集合。

因此,代码使用.where('Ravs name',isEqualTo:'English ')创建查询,这将创建文档的快照,我相信其中仅包含一个文档。如下所示:

itemCount:snapshot.data.documents.length

等于1,这就是为什么在itemBuilder中获得一个迭代的原因。

我没有机会进行测试,但是我认为解决方案应该是:

  1. 更改itemCount:snapshot.data.documents[0].data['item'].length-或类似的东西-为您提供适当数量的项目以迭代槽。

  2. itemBuilder的第一行应设置为:DocumentSnapshot ds =snapshot.data.documents[0];这是因为您要在唯一一个文档中遍历数组。

如果它一次不能工作,您应该使用它...我基于您提供的内容可以运行的事实:)。>

我希望它会有所帮助!

© www.soinside.com 2019 - 2024. All rights reserved.