Flutter Firebase断言失败:第360行pos 10:'data!= null'

时间:2020-09-02 20:32:53

标签: firebase flutter google-cloud-firestore

Widget BlogsList() {
return Container(
  color: UniversalVariables.blackColor,
  child: blogsStream != null
      ? Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            StreamBuilder(
              stream: blogsStream,
              builder: (context, snapshot) {
                if (!snapshot.hasData || snapshot.data.documents.isEmpty) ;

                return ListView.builder(
                  
                    padding: EdgeInsets.symmetric(horizontal: 16),
                    itemCount: snapshot.data.documents.length,
                    shrinkWrap: true,
                    itemBuilder: (context, index) {
                      return BlogsTile(
                        
                        authorName: snapshot
                            .data.documents[index].data['authorName'],
                        title: snapshot.data.documents[index].data["title"],
                        description:
                            snapshot.data.documents[index].data['desc'],
                        imgUrl:
                            snapshot.data.documents[index].data['imgUrl'],
                      );
                    });
              },
            )
          ],
        )
      : Container(
          alignment: Alignment.center,
          child: CircularProgressIndicator(),
        ),
); }

我希望将其设为强制性。给红色屏幕并打开。我想解决这个问题。我正在使用Firebase提取数据。我在页面之间切换时遇到问题,但是无法解决我的错误。

1 个答案:

答案 0 :(得分:1)

发生错误是在您的构建器中,无论ListView是否为真,您都返回了snapshot.hasData

尝试

builder: (context, snapshot) {
    if (!snapshot.hasData || snapshot.data.documents.isEmpty){
       return Center(child: Text("Loadin..."));
     }

     return ListView.builder(    
       padding: EdgeInsets.symmetric(horizontal: 16),
       itemCount: snapshot.data.documents.length,
       shrinkWrap: true,
       itemBuilder: (context, index) {
            return BlogsTile(
               authorName: snapshot.data.documents[index].data['authorName'],
               title: snapshot.data.documents[index].data["title"],
               description:snapshot.data.documents[index].data['desc'],
               imgUrl:snapshot.data.documents[index].data['imgUrl'],
             );
        });
},