【问题标题】:The operator '[]' isn't defined for the type 'Object'. Try defining the operator '[]'没有为“对象”类型定义运算符“[]”。尝试定义运算符'[]'
【发布时间】:2021-05-11 04:11:48
【问题描述】:

我的代码在此行中我在从 firebase 访问用户名时遇到问题的错误下方给出

snapshot.data['username']

它给出了上面提到的错误

我知道访问地图数据的唯一方法是这样

FutureBuilder<Object>(
  future: FirebaseFirestore.instance
   .collection('users')
   .doc(userId)
   .get(),
  builder: (context, snapshot) {
    if (snapshot.connectionState == ConnectionState.waiting) {
      return Text("Loading...");
    }
    return Text(
      snapshot.data['username'],
      style: TextStyle(
        fontWeight: FontWeight.bold,
      ),
    );
  }
),

【问题讨论】:

  • 你解决了吗,我现在也有这个问题

标签: firebase flutter flutter-futurebuilder


【解决方案1】:

在新的flutter更新中,我们不需要添加.data()

我收到此错误,删除 .data() 后问题已解决。

FutureBuilder<Object>(
  future: FirebaseFirestore.instance
   .collection('users')
   .doc(userId)
   .get(),
  builder: (context, snapshot) {
    if (snapshot.connectionState == ConnectionState.waiting) {
      return Text("Loading...");
    }
    return Text(
      snapshot['username'],
      style: TextStyle(
        fontWeight: FontWeight.bold,
      ),
    );
  }
),

【讨论】:

  • 请为您的回答提供一些背景信息,这将有助于其他人轻松理解您的方法。
  • @PawaraSiriwardhane 我猜 Shamil 说的是,snapshot.data()['username'] 可以替换为 snapshot['username']。
  • @ZubliQuzaini 答案不包含除代码之外的任何上下文。这就是为什么要求提供一些描述以及代码。后来他编辑了答案并添加了一些描述。
  • 啊,我明白了,我的错然后@PawaraSiriwardhane
【解决方案2】:

在较新版本的 Flutter 中,您必须将 FutureBuilder 的类型指定为 DocumentSnapshot。编辑您的代码,如下所示:

           FutureBuilder<DocumentSnapshot>(
              future: FirebaseFirestore.instance
                  .collection('users')
                  .doc(userId)
                  .get(),
              builder: (context, snapshot) {
                if (snapshot.connectionState == ConnectionState.waiting) 
                {
                  return const Text('Loading...');
                }
                return Text(
                  snapshot.data!['username'],
                  style: const TextStyle(fontWeight: FontWeight.bold),
                );
              }),

这应该适合你。

【讨论】:

    【解决方案3】:

    snapshot.data是FutureBuilder返回的数据。所以技术上snapshot.data 是DocumentSnapshot 的一种。要访问此文档的数据,您应该使用snapshot.data.data() 或以下代码:

    FutureBuilder<Object>(
      future: FirebaseFirestore.instance
       .collection('users')
       .doc(userId)
       .get(),
      builder: (context, snapshot) {
        if (snapshot.connectionState == ConnectionState.done) {
          DocumentSnapshot doc = snapshot.data;
          return Text(
            doc.data()['username'],
            style: TextStyle(
              fontWeight: FontWeight.bold,
            ),
          );
        }
        return Text("Loading...");
      }
    ),
    

    【讨论】:

      【解决方案4】:

      对于更新版本,您必须将 FutureBuilder 的类型指定为 DocumentSnapshot。 data 也更改为 requiredData。 编辑您的代码,如下所示:

             FutureBuilder<DocumentSnapshot>(
                future: FirebaseFirestore.instance
                    .collection('users')
                    .doc(userId)
                    .get(),
                builder: (context, snapshot) {
                  if (snapshot.connectionState == ConnectionState.waiting) 
                  {
                    return const Text('Loading...');
                  }
                  return Text(
                    snapshot.requiredData['username'],
                    style: const TextStyle(fontWeight: FontWeight.bold),
                  );
                }),
      

      现在您的代码将完美运行。

      【讨论】:

        【解决方案5】:

        在 Flutter 的更新版本中使用这种格式。

        FutureBuilder<DocumentSnapshot>(
                          future: FirebaseFirestore.instance
                              .collection('users')
                              .doc(userId)
                              .get(),
                          builder: (context, snapshot) {
                            if (snapshot.connectionState == ConnectionState.waiting) {
                              return Text("Loading..");
                            }
                            return Text(
                              snapshot.requireData['username'],
                              style: TextStyle(fontWeight: FontWeight.bold),
                            );
                          }),
        

        此解决方案适用于 Flutter 的 Udemy 课程 Flutter SDK 和 Flutter 框架的完整指南 4:30mins 第 336 课原生 iOS 和 Android 应用。 由 Maximilian Schwarzmuller Academind 创建

        【讨论】:

          【解决方案6】:

          改变这个:

            snapshot.data['username'],
          

          进入这个:

            snapshot.data.data()['username'];
          

          data() 是一种方法

          【讨论】:

          • 没有为“对象”类型定义“数据”方法。尝试将名称更正为现有方法的名称,或定义名为“数据”的方法
          • FutureBuilder&lt;Object&gt;这里去掉类型
          【解决方案7】:

          这个解决方案对我有用!

          尝试切换

          snapshot.data['username']
          

          与

          (snapshot.data as DocumentSnapshot)['username']
          

          【讨论】:

            【解决方案8】:

            这很容易做到,而且对我有用。

              FutureBuilder<Object>(
              future: FirebaseFirestore.instance
               .collection('users')
               .doc(userId)
               .get(),
              builder: (context, snapshot) {
               var data=snapshot.data as Map;
                if (snapshot.connectionState == ConnectionState.waiting) {
                  return Text("Loading...");
                }
                return Text(
                  data['username'],
                  style: TextStyle(
                    fontWeight: FontWeight.bold,
                  ),
                );
              }
            ),
            

            【讨论】:

              猜你喜欢
              • 2021-10-07
              • 1970-01-01
              • 1970-01-01
              • 2021-07-30
              • 1970-01-01
              • 1970-01-01
              • 2021-04-03
              • 1970-01-01
              • 2021-06-11
              相关资源
              最近更新 更多