开发者问题收集

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

2020-09-02
883
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个回答

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

尝试

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'],
             );
        });
},
Josteve Adekanbi
2020-09-02