如何解决错误“此函数的返回类型为Column,但不以return语句结尾”

时间:2019-08-29 10:01:26

标签: flutter google-cloud-firestore

正在使用演示聊天应用程序,尝试从Firestore返回数据。并收到错误消息。

child: Column(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      crossAxisAlignment: CrossAxisAlignment.stretch,
      children: <Widget>[
        StreamBuilder<QuerySnapshot>(
          stream: _firestore.collection('messagees').snapshots(),
          builder: (context, snapshot) {
            if (snapshot.hasData) {
              final messages = snapshot.data.documents;
              List<Text> messageWidgets = [];
              for (var message in messages) {
                final messageText = message.data['text'];
                final messageSender = message.data['sender'];
                final messageWidget =
                    Text('$messageText from $messageSender');
                messageWidgets.add(messageWidget);
              }

              return Column(
                children: messageWidget,
              );
            }
          },
        ),

我确实有一个return语句,所以不应该得到这个错误

3 个答案:

答案 0 :(得分:1)

仅当(snapshot.hasData)为true时,才会返回return语句。

如果语句为假,则不指定返回的内容。

建议:“ if-block”后面可能有一个return语句,该语句返回null(或其他值),当“ if-statement”为false时将满足该条件。

然后,在返回值的接收端,检查数据是否为“空”。如果数据不是“空”,则接收到的值实际上是“列”。

答案 1 :(得分:1)

如果快照没有数据,您还没有编写返回类型窗口小部件

child: Column(
  mainAxisAlignment: MainAxisAlignment.spaceBetween,
  crossAxisAlignment: CrossAxisAlignment.stretch,
  children: <Widget>[
    StreamBuilder<QuerySnapshot>(
      stream: _firestore.collection('messagees').snapshots(),
      builder: (context, snapshot) {
        if (snapshot.hasData) {
          final messages = snapshot.data.documents;
          List<Text> messageWidgets = [];
          for (var message in messages) {
            final messageText = message.data['text'];
            final messageSender = message.data['sender'];
            final messageWidget =
                Text('$messageText from $messageSender');
            messageWidgets.add(messageWidget);
          }

          return Column(
            children: messageWidget,
          );
        }else{ // put this else block
          return Container(
             child: Text('No Data Found'),
          ),
         }
      },
    ),

答案 2 :(得分:0)

你可以试试吗?

如果您有日志可以共享?

static List<Text> messageWidgets = []; // out of build

child: Column(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      crossAxisAlignment: CrossAxisAlignment.stretch,
      children: <Widget>[
        StreamBuilder<QuerySnapshot>(
          stream: _firestore.collection('messagees').snapshots(),
          builder: (context, snapshot) {
            if (snapshot.hasData) {
              final messages = snapshot.data.documents;
              for (var message in messages) {
                final messageText = message.data['text'];
                final messageSender = message.data['sender'];
                final messageWidget =
                    Text('$messageText from $messageSender');
                messageWidgets.add(messageWidget);
              }

              return (messageWidget ==  null || messageWidget.isEmpty) ? Text("List is empty") : Column(
                children: messageWidget,
              );
            }
          },
        ),