实施ListView.builder()时此代码中是否有任何问题

时间:2020-02-22 16:06:44

标签: firebase flutter google-cloud-firestore

为什么ListView.builder只显示2个数据,我在cloud_firestore中有这么多文档,我想检索这些数据并将其显示在ListView中,但实际上发生的是什么并不重要,它有多少个文档返回所有文档,但是当我使用ListView.builder使用小部件显示数据时,它仅显示2个小部件。

 Future<List<DocumentSnapshot>> getData() async {
var firestore = Firestore.instance;
QuerySnapshot qn =
    await firestore.collection("LiveProducts").getDocuments();
return qn.documents;

}

 Widget build(BuildContext context) {
return Container(
    child: FutureBuilder(
        future: getData(),
        builder: (_, AsyncSnapshot<List<DocumentSnapshot>> snapshot) {
          if (snapshot.connectionState == ConnectionState.waiting) {
            return Padding(
              padding: const EdgeInsets.only(
                top: 295,
              ),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: <Widget>[
                  Center(
                    child: SpinKitCircle(
                      color: Color.fromRGBO(91, 74, 127,10),
                      size: 50.0,
                    ),
                  ),
                ],
              ),
            );
          } else {
            return ListView.builder(
              // title: Text(snapshot.data[index].data["ProductName"]),
              shrinkWrap: true,
              itemCount: snapshot.data.length,
              itemBuilder: (_, index) {
                // if (snapshot.data[index].data["live"] == true) {
                  print(snapshot.data.length);
                    return Container(
                      margin: EdgeInsets.all(15),
                      height: 300,
                      decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(20),
                        color: Colors.green,
                      ),
                      child: Center(child: Text('$index'),),
                    );
                // }
              },
            );
          }
        }));

}

一件可怕的事情是笨拙,我使用的是Text Widget而不是Container Widget,它可以完美显示所有文档。像这样

  return Center(child:Text(snapshot.data[index].data["ProductName"])),

1 个答案:

答案 0 :(得分:0)

尝试以下操作:

else if(snapshot.connectionState == ConnectionState.done)  {
            return ListView.builder(
              // title: Text(snapshot.data[index].data["ProductName"]),
              shrinkWrap: true,
              itemCount: snapshot.data.length,
              itemBuilder: (_, index) {
                // if (snapshot.data[index].data["live"] == true) {
                  print(snapshot.data.length);
                    return Container(
                      margin: EdgeInsets.all(15),
                      height: 300,
                      decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(20),
                        color: Colors.green,
                      ),
                      child: Center(child: Text('$index'),),
                    );
                // }
              },
            );
          }

完成→const ConnectionState

已连接到终止的异步计算。