流生成器返回StreamBuilder <QuerySnapshot>而不是其中的文本

时间:2020-07-10 07:21:29

标签: flutter google-cloud-firestore stream-builder

  Widget getLastMessage(String userId, String peerId) {
    String groupChatId = '';

    if (userId.hashCode <= peerId.hashCode) {
      groupChatId = '$userId-$peerId';
    } else {
      groupChatId = '$peerId-$userId';
    }

    return StreamBuilder(
      stream: _firestore
      .collection('messages')
      .document(groupChatId)
      .collection(groupChatId)
      .orderBy('timestamp', descending: true)
      .limit(1)
      .snapshots(),
      builder: (context, snapshot) {
        if (!snapshot.hasData) {
          return Text('Loading...');
        } else {
          var documentsData = snapshot.data.documents;
          if (documentsData[0]['type'] == 1) {
           return Text('Image message');
          } else {
           return Text(documentsData[0]['content']);
          }
        }
      }
    );
  }

此代码仅返回StreamBuilder,就像该函数在所有打印语句都不起作用时未进入streambuilder

0 个答案:

没有答案