【问题标题】:How can I resolve " Error: The operator '[]' isn't defined for the class 'Object?'" [duplicate]如何解决“错误:未为类'对象定义运算符'[]'” [重复]
【发布时间】:2023-03-19 23:18:01
【问题描述】:
appBar: AppBar(
    title: StreamBuilder<DocumentSnapshot>(
        stream:  _firestore.collection('users').doc(userMap['uid']).snapshots(),
        builder: (context, snapshot){
          if (snapshot.data != null){
            return Container(
              child: Column(
                children: [
                  Text(userMap['name']),
                  Text(
                    snapshot.data!.data()['status'],
                    style: TextStyle(fontSize: 14),
                  )
                ],
              ),
            );
          }else{
            return Container();
          }
        }

我尝试检查用户的状态并在用户在线或离线时显示在应用栏中,我已经为此创建了一个变量并且它工作正常但是当我想在应用栏中显示状态时我有这个错误。

snapshot.data!.data()['status'],

错误在这一行 如果 somoene 能帮上忙,那就太好了。

【问题讨论】:

    标签: firebase flutter android-studio dart


    【解决方案1】:

    这样做,将整个对象放在一组括号中:

    (snapshot.data!.data())['status'],
    

    【讨论】:

      【解决方案2】:

      试试这个: (snapshot.data!.data() as Map&lt;String, dynamic&gt;)['status'],

      Dart 不知道它得到的是哪种类型的对象。

      见参考: The operator '[]' isn't defined for the class 'Object'. Dart

      【讨论】:

        猜你喜欢
        • 2021-07-31
        • 1970-01-01
        • 2021-07-30
        • 2021-07-30
        • 1970-01-01
        • 2021-08-18
        • 2022-06-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多