【问题标题】:Not able to access the data from firebase using StreamBuilder in flutter无法在flutter中使用StreamBuilder访问firebase中的数据
【发布时间】:2020-05-25 06:51:30
【问题描述】:

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

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

没有错误,只是一个空白页。

     @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();
          }
        },
      )),

【问题讨论】:

  • 尝试打印_document
  • @JohnJoe 什么都没有打印出来。
  • 尝试调试。有没有到这个if (snapshot.hasData)
  • @JohnJoe.Tried 也这样做了。不,它没有转到 if (snapshot.hasData)。
  • 那么它去哪儿了?

标签: firebase flutter dart google-cloud-firestore mobile-development


【解决方案1】:

我认为您的快照存在一些错误,但您在此处访问 Firestore 数据的方式也存在错误。您必须访问 DocumentSnapshot 的 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',
);

【讨论】:

  • @Harsh 我确定firestore的安全规则有错误。能否复制粘贴到问题中?
  • 你想让我把什么复制粘贴到问题中?
  • @Harsh 你知道 Firestore 中有哪些安全规则吗?我认为它有问题,所以我想看看它来解决你的问题。您能否转到您的 Firebase 控制台并复制并粘贴安全规则,以便我解决您的问题?
  • rules_version = '2'; service cloud.firestore { match /databases/{database}/documents { match /{document=**} { 允许读取,写入:如果为真; } } }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-12-19
  • 2021-05-07
  • 2021-09-30
  • 1970-01-01
  • 2020-12-14
  • 2020-10-10
  • 2019-04-13
相关资源
最近更新 更多