【问题标题】:How to fix error with new Firestore version?如何修复新 Firestore 版本的错误?
【发布时间】:2023-03-31 04:35:01
【问题描述】:

嘿,我最近将我的 cloud_firestore 依赖项从版本 0.12.9+4 更新到了 2.2.1。现在我必须对我的代码进行一些更改。我已经阅读了如何迁移 2.0.0 版的代码并更改了部分代码。但我不知道如何在这部分更改它:

 //imbiss list from snapshot
  List<Imbiss> _imbissListFromSnapshot(QuerySnapshot snapshot) {
    return snapshot.docs.map((doc) {
      return Imbiss(
        name: doc.data["name"],
        kind: doc.data["kind"],
        location: doc.data["location"],
        rating: doc.data["rating"],
        ratingFood: doc.data["ratingFood"],
        ratingLocation: doc.data["ratingLocation"],
        ratingPLV: doc.data["ratingPLV"],
        ratingService: doc.data["ratingService"],
        date: doc.data["date"],
        user: doc.data["user"],
      );
    }).toList();
  }

代码之前运行得非常好,现在我在 doc.data 之后的括号中出现错误。 Android Studio 告诉我:没有为类型“Object Function()”定义运算符“[]”。你知道我必须做什么吗?

【问题讨论】:

    标签: firebase flutter dart google-cloud-firestore


    【解决方案1】:

    试试这个:

      List<Imbiss> _imbissListFromSnapshot(AsyncSnapshot<QuerySnapshot> snapshot) {
        return snapshot.docs.map((doc) {
          Map<String, dynamic> newDoc = doc.data();
          return Imbiss(
            name: newDoc["name"],
            kind: newDoc["kind"],
            location: newDoc["location"],
            rating: newDoc["rating"],
            ratingFood: newDoc["ratingFood"],
            ratingLocation: newDoc["ratingLocation"],
            ratingPLV: newDoc["ratingPLV"],
            ratingService: newDoc["ratingService"],
            date: newDoc["date"],
            user: newDoc["user"],
          );
        }).toList();
      }
    

    【讨论】:

    • 这个对我有用,但我使用 QuerySnapshot 而不是 AsyncSnapshot。而且我还必须像 sungkd123 的回答一样传递默认值
    【解决方案2】:

    将此用于 QuerySnapshot,'' 在 ??是传递的默认值,请根据数据类型更改默认值

     //imbiss list from snapshot
      List<Imbiss> _imbissListFromSnapshot(QuerySnapshot snapshot) {
        return snapshot.docs.map((doc) {
          return Imbiss(
            name: doc.get("name") ?? '' ,
            kind: doc.get("kind") ?? '',
            location: doc.get("location") ?? '',
            rating: doc.get("rating") ?? '',
            ratingFood: doc.get("ratingFood") ?? '' ,
            ratingLocation: doc.get("ratingLocation") ?? '' ,
            ratingPLV: doc.get("ratingPLV")  ?? '' ,
            ratingService: doc.get("ratingService") ?? '' ,
            date: doc.get("date") ?? '' ,
            user: doc.get("user") ?? '',
          );
        }).toList();
      }
    

    这用于 DocumentSnapshot

      //imbiss list from snapshot
          List<Imbiss> _imbissListFromSnapshot(DocumentSnapshot snapshot) {
            return snapshot.docs.map((doc) {
              return Imbiss(
                name: snapshot.get("name")  ,
                kind: snapshot.get("kind") ,
                location: snapshot.get("location") ,
                rating: snapshot.get("rating"),
                ratingFood: snapshot.get("ratingFood"),
                ratingLocation: snapshot.get("ratingLocation") ,
                ratingPLV: snapshot.get("ratingPLV")  ,
                ratingService: snapshot.get("ratingService") ,
                date: snapshot.get("date"),
                user: snapshot.get("user"),
              );
            }).toList();
          }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-01-09
      • 2020-10-27
      • 1970-01-01
      • 1970-01-01
      • 2012-12-19
      • 2012-06-05
      • 2023-03-20
      相关资源
      最近更新 更多