【问题标题】:After updating cloud firestore: The operator '[]' isn't defined for the type 'Object'. Try defining the operator '[]'更新云 Firestore 后:未为“对象”类型定义运算符“[]”。尝试定义运算符'[]'
【发布时间】:2021-08-08 00:32:21
【问题描述】:

一切都运行良好,但是当我升级我的云火库依赖时。我开始收到错误消息“未为类型'Object'定义运算符'[]'。”。这个错误出现在所有 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
      
    });
  }

【问题讨论】:

  • 尝试使用print 语句来检查该数据是什么。您可能必须先jsonDecode
  • 打印(userData.data());何时使用此打印我得到完整的 json 数据但是当我尝试获取特定数据时它显示错误。如果我使用此依赖项,它不会显示任何错误,并且会顺利运行 cloud_firestore 1.0.7。但是升级后错误来了 print(userData.data()["name"]);
  • 如果 print(userData) 它会给出'_JsonDocumentSnapshot'的实例
  • 地图 user = jsonDecode(userData.data()); print(user["name"].toString());这个也试过了还是不行

标签: firebase flutter dart google-cloud-firestore


【解决方案1】:

cloud_firestore 2.0.0 版开始

DocumentSnapshot 类现在采用通用参数。声明:

abstract class DocumentSnapshot<T extends Object?> {

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

  T? data();

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

    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&lt;String,dynamic&gt; 类型,您可以像往常一样使用[] 运算符访问数据。


另一个例子:

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

上面会报错:

“Stream>”类型的值不能分配给“Stream>>”类型的变量。

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

abstract class Query<T extends Object?>

snapshots() 返回以下内容:

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

由于没有为QueryT extends Object? 指定类型,因此在代码中snapshots() 将具有以下返回类型Stream&lt;QuerySnapshot&lt;Object?&gt;&gt;,您将收到上述错误。所以要解决这个问题,你必须这样做:

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

根据docs

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

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

【讨论】:

    【解决方案2】:

    就我而言,我只需将 snapshot.data()['parameter'] 更改为 snapshot.get('parameter')

    UserModel _userFromFirebaseSnapshot(DocumentSnapshot snapshot) {
     return snapshot != null ?
        UserModel(snapshot.id,
          name: snapshot.get('name'),
          profileImageUrl: snapshot.get('profileImageUrl'),
          email: snapshot.get('email'),
        ) : null;
     }
    

    【讨论】:

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