【问题标题】:why there is an error in line 15 while accessing a field from firebase in my flutter chat app为什么在我的颤振聊天应用程序中从 firebase 访问字段时第 15 行出现错误
【发布时间】:2021-09-13 14:30:25
【问题描述】:

我的问题在第 15 行,同时访问数据 ['author_id'] 但 android studio 说

错误:没有为“对象”类型定义运算符“[]”。 (undefined_operator at [shaheb_chats] lib\src\widgets\message_wall.dart:23)

 class MessageWall extends StatelessWidget {

  final List<QueryDocumentSnapshot> messages;
  


  const MessageWall({Key key,this.messages}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
      itemCount: messages.length,
      itemBuilder: (context, index){
        final data =  messages[index].data();
        final user = FirebaseAuth.instance.currentUser;
        if(user != null && user.uid == data["author_id"]){
          return ChatMessage(
            index: index,
            data: data,
          );
        }
        return ChatMessageOther(
          index: index,
          data: data,
        );
      },
    );
  }
}

【问题讨论】:

  • 消息列表为空,能否提供一下如何在列表中推送消息的代码

标签: firebase flutter


【解决方案1】:

显然你调用data()返回一个对象,而你期望它返回一个地图。

转换类型的快速方法是:

final data =  messages[index].data() as Map;

此错误是由 Firestore 插件 2.0 版本的更改引起的,我建议阅读有关 migrating to cloud_firestore 2 的文档。

【讨论】:

    猜你喜欢
    • 2020-03-15
    • 2021-05-01
    • 2022-01-09
    • 2019-01-25
    • 2021-01-04
    • 2021-03-20
    • 1970-01-01
    • 2020-08-20
    • 2021-11-28
    相关资源
    最近更新 更多