首页 > 解决方案 > 颤动的火力库检索数据快照时出错.data.docs.length

问题描述

我正在使用流生成器从 firestore 中的 flutter firebase 中检索数据,但在snapshot.data.docs.lengt. 如果您能帮我解决,我将不胜感激。

这是我的代码

StreamBuilder(
  stream: FirebaseFirestore.instance
      .collection(
      'Showrooms/${ConstantsFirebaseMethods.getCurrentUserId()}/Vehicles')
      .snapshots(),
  builder: (_, snaphot) {
    print(
        'currentUserId ${ConstantsFirebaseMethods.getCurrentUserId()}');
    return GridView.builder(
        scrollDirection: Axis.vertical,
        gridDelegate: SliverGridDelegateWithMaxCrossAxisExtent(
            maxCrossAxisExtent: 200, mainAxisSpacing: 10),
        itemCount: snaphot.data.docs.length,
        itemBuilder: (BuildContext ctx, index) {
          print(
              ' here is levelName ${snaphot.data.docs[index].get('name')} and levelDuration ${snaphot.data!.docs[index].get('duration')}');
          return InkWell(
            onTap: () {},
            child: Container(
                margin: EdgeInsets.only(
                    top: 8.0, left: 8.0, right: 8.0),
                decoration: BoxDecoration(
                  boxShadow: [
                    BoxShadow(
                      color: Colors.grey.withOpacity(0.8),
                      spreadRadius: 2,
                      blurRadius: 3,
                      offset: Offset(
                          0, 3), // changes position of shadow
                    ),
                  ],
                  borderRadius:
                  BorderRadius.all(Radius.circular(25.0)),
                  color: Colors.white,
                ),
                padding: EdgeInsets.only(top: 50.0),
                width: MediaQuery.of(context).size.width * 0.6,
                height: MediaQuery.of(context).size.height * 0.20,
                child: Column(
                  children: [
                    Container(
                      child: Text(
                        '${snaphot.data.docs[index].get('name')}',
                        textAlign: TextAlign.center,
                        style: TextStyle(
                            fontWeight: FontWeight.w600,
                            fontSize: 26.0),
                      ),
                    ),
                    Container(
                      child: Text(
                        '${snaphot.data.docs[index].get('duration')}',
                        textAlign: TextAlign.center,
                        style: TextStyle(
                            fontWeight: FontWeight.w600,
                            fontSize: 16.0),
                      ),
                    ),
                  ],
                )),
          );
        });
  }),

标签: firebasefluttergoogle-cloud-firestoresnapshot

解决方案


推荐阅读