更新 cloud firestore 后:未为类型“Object"定义运算符“[]".尝试定义运算符“[]" [英] After updating cloud firestore: The operator '[]' isn't defined for the type 'Object'. Try defining the operator '[]'

查看:36
本文介绍了更新 cloud firestore 后:未为类型“Object"定义运算符“[]".尝试定义运算符“[]"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一切正常,但是当我升级我的云 Firestore 依赖项时.我开始收到错误消息未为类型‘对象’定义运算符‘[]’.".此错误出现在所有 4 个 userData.data()[""],

Every thing was working good but when i upgraded my cloud firestore dependecy. I started getting an error "The operator '[]' isn't defined for the type 'Object'. ". This error is coming in front of all the 4 userData.data()[""],

class BaseProvider with ChangeNotifier {
  //instances of firebase

  final FirebaseAuth _auth = FirebaseAuth.instance;

  final CollectionReference postsCollection =
      FirebaseFirestore.instance.collection("posts");

  final CollectionReference userCollection =
      FirebaseFirestore.instance.collection("users");

  //Creating post

  Future addPost(
    
  ) async {
    DocumentSnapshot userData =
        await userCollection.doc(_auth.currentUser.uid).get();
    return await postsCollection.doc().set({
      "id": _auth.currentUser.uid,
      "sellername": userData.data()["name"],      //Error
      "sellercontact": userData.data()["phone"],  //Error
      "sellercity": userData.data()["city"],      //Error
      "sellerstate": userData.data()["state"],    //Error
      
    });
  }

推荐答案

cloud_firestore Version 2.0.0

开始

DocumentSnapshot 类现在接受一个通用参数.声明:

Starting at cloud_firestore Version 2.0.0

The class DocumentSnapshot now takes a generic parameter. The declaration:

abstract class DocumentSnapshot<T extends Object?> {

因此它包含一个 T 类型的抽象方法:

and therefore it contains an abstract method of type T:

  T? data();

因此您需要执行以下操作:

Therefore you need to do the following:

    DocumentSnapshot<Map<String, dynamic>> userData =
        await userCollection.doc(_auth.currentUser.uid).get();
    return await postsCollection.doc().set({
      "id": _auth.currentUser.uid,
      "sellername": userData.data()["name"],      
      "sellercontact": userData.data()["phone"],  
      "sellercity": userData.data()["city"],      
      "sellerstate": userData.data()["state"], 
      
    });

现在 data() 方法将是 Map 类型,您可以像平常一样使用 [] 运算符.

Now data() method will be of type Map<String,dynamic> and you can access the data as you normally do using the [] operator.

Query query = FirebaseFirestore.instance.collection("collectionPath");
final Stream<QuerySnapshot<Map<String,dynamic>>> snapshots = query.snapshots();

上面会报错:

Stream"类型的值不能分配给类型为Stream>>"的变量.

A value of type 'Stream<QuerySnapshot<Object?>>' can't be assigned to a variable of type 'Stream<QuerySnapshot<Map<String, dynamic>>>'.

您收到此错误是因为 Query 具有以下声明:

You get this error because Query has the following declaration:

abstract class Query<T extends Object?>

snapshots() 返回以下内容:

Stream<QuerySnapshot<T>> snapshots({bool includeMetadataChanges = false});

由于没有为 Query 指定类型,并且由于 T extends Object?,因此在代码中 snapshots() 将具有以下返回类型 Stream> 并且您将收到上述错误.所以要解决这个问题,你必须这样做:

Since a type wasn't specified for Query and since T extends Object?, therefore in the code snapshots() will have the following return type Stream<QuerySnapshot<Object?>> and you will get the above error. So to solve this you have to do:

Query<Map<String,dynamic>> query = FirebaseFirestore.instance.collection("collectionPath");
final Stream<QuerySnapshot<Map<String,dynamic>>> snapshots = query.snapshots();


根据文档:

破坏性重构:DocumentReference、CollectionReference、Query、DocumentSnapshot、CollectionSnapshot、QuerySnapshot、QueryDocumentSnapshot、Transaction.get、Transaction.set 和 WriteBatch.set 现在采用额外的通用参数.(#6015).

BREAKING REFACTOR: DocumentReference, CollectionReference, Query, DocumentSnapshot, CollectionSnapshot, QuerySnapshot, QueryDocumentSnapshot, Transaction.get, Transaction.set and WriteBatch.set now take an extra generic parameter. (#6015).

因此,您需要为所有这些类实现上述内容.

Therefore you need to implement the above for all those classes.

这篇关于更新 cloud firestore 后:未为类型“Object"定义运算符“[]".尝试定义运算符“[]"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆