Firebase Firestore 错误:未为“Object”类定义运算符“[]”

时间:2021-05-08 11:27:02

标签: firebase flutter dart google-cloud-firestore

我正在通过这个视频学习如何将 Flutter 应用程序连接到 Firebase

<块引用>

https://youtu.be/ggYTQn4WVuw

对我来说一切都完全一样,但在 Android Studio 中出现了错误。

<块引用>

错误:未为类型“Object”定义运算符“[]”。 (undefined_operator at [firebase_test] lib\services\database.dart:24)

错误代码:

List<Brew> _brewListFromSnapshot(QuerySnapshot snapshot) {
return snapshot.docs.map((doc) {
  return Brew(
    name: doc.data()['name'] ?? '',
    strength: doc.data()['strength'] ?? 0,
    sugars: doc.data()['sugars'] ?? '0',
  );
}).toList();}

酿造课:

class Brew {
  final String name;
  final String sugars;
  final int strength;

  Brew({ this.name, this.sugars, this.strength });
}

有人可以帮我解决这个问题吗?这是 Android Studio 的问题吗?

3 个答案:

答案 0 :(得分:9)

您使用的是 4 天前发布的最新 Firestore 依赖项 2.0.0 版。有重大变化,请阅读here

“添加 withConverter”,来自文档:

<块引用>

将 withConverter 函数添加到 CollectionReference、DocumentReference 和 Query (#6015)。这种新方法允许以类型安全的方式与集合/文档交互:

final modelsRef = FirebaseFirestore
     .instance
     .collection('models')
     .withConverter<Model>(
       fromFirestore: (snapshot, _) => Model.fromJson(snapshot.data()!),
       toFirestore: (model, _) => model.toJson(),
     );

 Future<void> main() async {
   // Writes now take a Model as parameter instead of a Map
   await modelsRef.add(Model());
   final Model model = await modelsRef.doc('123').get().then((s) => s.data());
 }

答案 1 :(得分:4)

使用这个:

Traceback (most recent call last):
   File <my python file in line where "service" is declared>
   File "googleapiclient\_helpers.py", line 134, in positional_wrapper
   File "googleapiclient\discovery.py", line 291, in build
   File "googleapiclient\discovery.py", line 405, in _retrieve_discovery_doc
googleapiclient.errors.UnknownApiNameOrVersion: name: sheets version: v4

代替:

doc.get('name') ?? '', 

答案 2 :(得分:1)

第一个解决方案:

您的 Brew 课程:

class Brew {
  final String name;
  final String sugars;
  final int strength;

  Brew({ this.name, this.sugars, this.strength });
  
  Brew.fromJson(Map<String, dynamic> json)
      : this(
          name: json['name'] as String,
          sugars: json['sugars'] as String,
          strength: json['strength'] as int,
        );

  Map<String, Object?> toJson() {
    return {
      'name': name,
      'sugars': sugars,
      'strength': strength,
    };
  }
}

您的服务:

List<Brew> _brewListCollectionReference (CollectionReference<Map<String, dynamic>> collectionReference) {
  QuerySnapshot<Object?> snapshot = await collectionReference.withConverter<Brew>(
          fromFirestore: (snapshots, _ ) => Recipe.fromJson(snapshots.data()!),
          toFirestore: (brew, _ ) => brew.toJson(),
        ).get();
  return snapshot.docs.map((doc) => doc.data() as Brew).toList();
}

注意:您的“collectionReference”是来自您的 Firebase 集合的参考。

例如: firestore.collection('brewCollection')

第二种解决方案:

List<Brew> _brewListFromSnapshot(QuerySnapshot snapshot) {
return snapshot.docs.map((doc) {
  return Brew(
    name: doc['name'] ?? '',
    strength: doc['strength'] ?? 0,
    sugars: doc['sugars'] ?? '0',
  );
}).toList();}

第三种解决方案:

List<Brew> _brewListFromSnapshot(QuerySnapshot snapshot) {
return snapshot.docs.map((doc) {
  return Brew(
    name: doc.get('name') ?? '',
    strength: doc.get('strength') ?? 0,
    sugars: doc.get('sugars') ?? '0',
  );
}).toList();}