简体   繁体   English

StreamBuilder Flutter Firebase

[英]StreamBuilder Flutter Firebase

I am trying to implement a stream builder to display a String from a firebase document.我正在尝试实现一个流构建器来显示来自 firebase 文档的字符串。 However I get the following error "The body might complete normally, causing 'null' to be returned, but the return type is a potentially non-nullable type."但是,我收到以下错误“主体可能正常完成,导致返回 'null',但返回类型可能是不可为空的类型。” on the 3rd line of the code.在代码的第 3 行。 Not sure what I am doing wrong.不知道我做错了什么。 Any ideas?有任何想法吗?

    StreamBuilder<QuerySnapshot>(
          stream: _firestore.collection('data').snapshots(),
          builder: (context, snapshot) { 
            if (snapshot.hasError) {
              return const ErrorWidget();
            }
            if (snapshot.hasData) {
              final messages = snapshot.data!.docs;
              List<Text> messageWidget = [];
              for (var message in messages) {
                final messageText = message.get('email');
                final messageWidget = Text('$messageText');
              }
              return Column(
                children: messageWidget,
              );
            }
          },
        )

Add another return after and outside your second if statement.在第二个 if 语句之后和之外添加另一个返回。 Because currently your streambuilder only returns on two conditions, either error or hasData.因为目前您的流构建器仅在两个条件下返回,错误或 hasData。 But what about everything else?但其他一切呢? For example waiting?例如等待?

You can add return CircularProgressindicator() at the end, and it should work.您可以在最后添加return CircularProgressindicator() ,它应该可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM