【问题标题】:Breaking changes with cloud_firestore 2.0?cloud_firestore 2.0 的重大变化?
【发布时间】:2021-08-01 17:05:00
【问题描述】:

我在我的应用中使用 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 类型的变量”。

我不明白发生了什么变化......因为这一切以前都很好。

大家也遇到过这种情况吗?

【问题讨论】:

    标签: flutter google-cloud-firestore dart-null-safety


    【解决方案1】:

    有一个文档可以执行迁移: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()!;
    

    【讨论】:

    • 谢谢你的回答:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-24
    相关资源
    最近更新 更多