无法在Flutter中使用StreamBuilder从Firebase访问数据 [英] Not able to access the data from firebase using StreamBuilder in flutter

查看:105
本文介绍了无法在Flutter中使用StreamBuilder从Firebase访问数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里,我尝试使用streamBuilder中的firstore实例从firebase数据库中获取数据.

Here I am trying to get the data from the firebase database using firstore instance in streamBuilder.

  • .collection('/message_data/friendA ## friendB/message_list'):-这是我从firebase收集的文档路径

没有错误,只是空白页.

No error, just a blank page.

     @override
     Widget build(BuildContext context) {
       return Scaffold(
       appBar: AppBar(
       title: Text(widget.friendName),
     ),
     body: Column(
     children: <Widget>[
      Flexible(
          child: StreamBuilder(
        stream: Firestore.instance
            .collection('/message_data/friendA##friendB/message_list')
            .snapshots(),
        builder: (BuildContext context, AsyncSnapshot snapshot) {
          if (snapshot.hasError) {
            return Text('Error on chatView ${snapshot.error.toString()}');
          }
          if (snapshot.connectionState == ConnectionState.active) {
            if (snapshot.hasData) {
              if (snapshot.data.documents .length > 0) {
                return ListView.builder(

                  itemCount: snapshot.data.documents.length,
                  itemBuilder: (BuildContext context, int index) {
                    DocumentSnapshot _document = snapshot.data.documents[index];



                    return ChatMessage(
                      isFriend: _document['fromA'],

                      isNotPrevious:snapshot.data.documents.length - 1 == index,
                      message: _document['content'],
                      friendInitial: 'T',

                      avatarUrl:'https://avatarfiles.alphacoders.com/132/132399.jpg',
                    );
                  },
                );

              }
              else{
                return Text('No messages found in chat view length vala'); 
              }
            }
            else{
                return Text('No messages found in chat view hasdata'); 
              }
          }
          else{
            return CircularProgressIndicator();
          }
        },
      )),

推荐答案

我认为您的快照中存在一些错误,但是在此处访问Firestore数据的方式也存在错误.您必须访问DocumentSnapshot的data属性才能访问数据.

I think you have some error in your snapshot, but you also have an error in how you access your firestore data here. You have to access the data property of your DocumentSnapshot in order to access the data.

DocumentSnapshot _document = snapshot.data.documents[index];
Map chatData = _document.data;


return ChatMessage(
    isFriend: chatData['fromA'],

    isNotPrevious:snapshot.data.documents.length - 1 == index,
    message: chatData['content'],
    friendInitial: 'T',

    avatarUrl:'https://avatarfiles.alphacoders.com/132/132399.jpg',
);

这篇关于无法在Flutter中使用StreamBuilder从Firebase访问数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆