Flutter FutureBuilder返回零错误触发

h43kikqp  于 2023-01-14  发布在  Flutter
关注(0)|答案(2)|浏览(140)

我试图从Firestore数据库中的给定文档返回一个评论和文章字段。我使用一个未来的构建器通过ListTile返回此数据。
当我运行它的时候,屏幕在一瞬间变红,并呈现下面的错误,然后磁贴才成功地按预期显示。错误是:生成函数返回null。有问题的小部件为:FutureBuilder构建函数决不能返回null。要返回导致构建小部件填满可用空间的空白空间,请返回"new Container()"。要返回占用尽可能少空间的空白空间,请返回"new Container(width:0.0,高度:0.0)"。
下面是我的代码:

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';

class PostGetter extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
// TODO: implement build
return new Scaffold(
  body: new Container(
    child: new FutureBuilder(
        future: Firestore.instance
            .collection('post')
            .where('article', isEqualTo: 'lpquVtoRNsu0vBLjNByS')
            .getDocuments(),
        builder: (BuildContext context, AsyncSnapshot snapshot) {
          if (snapshot.hasData) {
            if (snapshot.data != null) {
              return new Column(
                children: <Widget>[
                  new Expanded(
                    child: new ListView(
                      children: snapshot.data.documents
                          .map<Widget>((DocumentSnapshot document) {
                        return new ListTile(
                          title: new Text(document['comment']),
                          subtitle: new Text(document['author']),
                        );
                      }).toList(),
                    ),
                  ),
                ],
              );
            } else {
              return new CircularProgressIndicator();
            }
          }
        }),),
);
  }
}

为什么循环进度指示器不能满足返回小部件的需要?
谢谢你的帮助。

6rvt4ljy

6rvt4ljy1#

试试这个:

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';

class PostGetter extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
// TODO: implement build
return new Scaffold(
  body: new Container(
    child: new FutureBuilder(
        future: Firestore.instance
            .collection('post')
            .where('article', isEqualTo: 'lpquVtoRNsu0vBLjNByS')
            .getDocuments(),
        builder: (BuildContext context, AsyncSnapshot snapshot) {
          if (snapshot.hasData) {
            if (snapshot.data != null) {
              return new Column(
                children: <Widget>[
                  new Expanded(
                    child: new ListView(
                      children: snapshot.data.documents
                          .map<Widget>((DocumentSnapshot document) {
                        return new ListTile(
                          title: new Text(document['comment']),
                          subtitle: new Text(document['author']),
                        );
                      }).toList(),
                    ),
                  ),
                ],
              );
            } 
          }else {
              return new CircularProgressIndicator();
            }
        }),),
);
  }
}

else应该在快照上。hasdata不在www.example.com上!= nullsnapshot.data != null

jogvjijk

jogvjijk2#

return FutureBuilder(
          future: FirebaseFirestore.instance.collection('images').doc("walpaper").get(),
          builder: (context, snapshot) {
            if (snapshot.hasData) {

          ////////////////Chack here///////////////

              if (snapshot.data!.data() == null) {
                return SizedBox();
              }

相关问题