cloud_firestore 2.0 的重大变化?

Breaking changes with cloud_firestore 2.0?

我在我的应用程序中使用 CloudFirestore。 一切正常,自从 2.0.0 版本以来,我遇到了以前没有遇到的错误。

这是代码:

   final _fireStore = FirebaseFirestore.instance
        .collection('familyAccounts')
        .doc(id)
        .collection('users');

    final DocumentSnapshot doc1 = await _fireStore.doc('user1').get();
    final DocumentSnapshot doc2 = await _fireStore.doc('user2').get();

    final _fireStore2 = FirebaseFirestore.instance
        .collection('familyAccounts')
        .doc(id)
        .collection('users')
        .doc('user1')
        .collection('vocList');
    await _fireStore2.get().then((QuerySnapshot querySnapshot) {
      querySnapshot.docs.forEach((doc) {
        _carnetVoc1.add(
          VocList(
              ref: doc['ref'],
              titre: doc['titre'],
              creation: doc['dateCreation'],
              modification: doc['dateModification'],
              wordId: doc['mots']),
        );
      });
    });
    final _fireStore3 = FirebaseFirestore.instance
        .collection('familyAccounts')
        .doc(id)
        .collection('users')
        .doc('user2')
        .collection('vocList');
    await _fireStore3.get().then((QuerySnapshot querySnapshot) {
      querySnapshot.docs.forEach((doc) {
        _carnetVoc2.add(
          VocList(
              ref: doc['ref'],
              titre: doc['titre'],
              creation: doc['dateCreation'],
              modification: doc['dateModification'],
              wordId: doc['mots']),
        );
      });
    });

    _accountEmail = id;

    Map user1 = doc1.data()!;
    Map user2 = doc2.data()!;

    _user1 = User(
        userId: user1['userId'],
        avatar: user1['avatar'],
        classe: user1['classe'],
        teacherCode: user1['teacherCode'],
        carnetVoc: _carnetVoc1);

    _user2 = User(
        userId: user2['userId'],
        avatar: user2['avatar'],
        classe: user2['classe'],
        teacherCode: user2['teacherCode'],
        carnetVoc: _carnetVoc2);

台词: 映射 user1 = doc1.data()!; 映射 user2 = doc2.data()!; 不再使用新版本:我明白了:

“不能将对象类型的值分配给 Map 类型的变量”。

我不明白发生了什么变化...因为之前一切正常。

有人遇到过这个吗?

有一个文档要执行迁移:https://firebase.flutter.dev/docs/firestore/2.0.0_migration/

引用它,你应该明确地添加类型<Map<String, dynamic>>

在您的情况下,您需要更改:


    final DocumentSnapshot doc1 = await _fireStore.doc('user1').get();
    final DocumentSnapshot doc2 = await _fireStore.doc('user2').get();

至:


    final DocumentSnapshot<Map<String,dynamic>> doc1 = await _fireStore.doc('user1').get();
    final DocumentSnapshot<Map<String,dynamic>> doc2 = await _fireStore.doc('user2').get();

此外,cloud_firestore: 2.0.0 促进了类型安全,因此我建议您为变量使用 Map 具体类型:

    Map<String,dynamic> user1 = doc1.data()!;
    Map<String,dynamic> user2 = doc2.data()!;