【问题标题】:The operator '[]' isn't defined for the type 'Object? Function()'. Try defining the operator '[]'没有为类型“对象”定义运算符“[]”?功能()'。尝试定义运算符'[]'
【发布时间】:2021-10-07 10:53:18
【问题描述】:

我最近开始了一个颤振项目,并将一些旧的 firebase 代码迁移到新的。然后我更新了 firebase 并修复了大部分错误,直到这个问题难倒了我。

final firestore = FirebaseFirestore.instance; //
FirebaseAuth auth = FirebaseAuth.instance;

Future<bool?> addPlastic(String amount) async {
  try {
    String uid = auth.currentUser!.uid;
    var value = double.parse(amount);

    DocumentReference documentReference =
        FirebaseFirestore.instance.collection('Users').doc(uid);

    FirebaseFirestore.instance.runTransaction((transaction) async {
      DocumentSnapshot snapshot = await transaction.get(documentReference);
      if (!snapshot.exists) {
        documentReference.set({'plasticsCollected': value});
        return true;
      }
      double newAmount = snapshot.data()!['plasticsCollected'] + value;
      transaction.update(documentReference, {'plasticsCollected': newAmount});

      return true;
    });
  } catch (e) {
    return false;
  }
}

这是我收到错误“未为类型 'Object?Function()' 定义运算符'[]'。尝试定义运算符'[]'。”

double newAmount = snapshot.data()!['plasticsCollected'] + value;

我做错了什么?

【问题讨论】:

    标签: firebase flutter google-cloud-firestore


    【解决方案1】:

    在搞砸了一点之后,我最终改变了:

    double newAmount = snapshot.data()!['plasticsCollected'] + value;
    

    double newAmount = snapshot.get('plasticsCollected') + value;
    

    现在它对我有用。

    【讨论】:

    • @YeasinSheikh 谢谢,我刚刚做了 q 和一个选项,因为这是我遇到的一个问题,只是想把它放在那里以防有人需要它。
    猜你喜欢
    • 2021-05-11
    • 1970-01-01
    • 1970-01-01
    • 2021-07-30
    • 1970-01-01
    • 1970-01-01
    • 2021-04-03
    • 1970-01-01
    • 2021-06-11
    相关资源
    最近更新 更多