【问题标题】:Flutter & Firebase: type 'Query' is not a subtype of type 'CollectionReference'Flutter & Firebase:“Query”类型不是“CollectionReference”类型的子类型
【发布时间】:2020-04-02 08:17:27
【问题描述】:

在 Firestore 中,我有一个名为“习惯”的集合,每个文档都有一个包含用户 ID 的数组。我现在想获得一个集合,其中包含数组中包含特定用户 ID 的所有习惯。

这是我的代码:

final CollectionReference habitDataCollection = Firestore.instance.collection('habits').where("habitFollowers", arrayContains: 'userID');

现在,我收到此错误:“Query”类型不是“CollectionReference”类型的子类型

你知道我在这里做错了什么吗?

非常感谢您的帮助!

尼古拉斯

PS:

然后代码使用 Stream 获取快照

  Stream<List<HabitData>> get habitData {
    return habitDataCollection.snapshots()
      .map(_habitDataListFromSnapshot);
  }

并将其形成一个飞镖对象

  List<HabitData> _habitDataListFromSnapshot(QuerySnapshot snapshot) {
    return snapshot.documents.map((doc){
     return HabitData(
       hid: doc.documentID ?? '',
       name: doc.data['name'] ?? '',
       description: doc.data['description'] ?? '',
       );
    }).toList();
  }

【问题讨论】:

    标签: firebase flutter dart google-cloud-firestore


    【解决方案1】:

    改变这个:

    final CollectionReference habitDataCollection = Firestore.instance.collection('habits').where("habitFollowers", arrayContains: 'userID');
    

    进入这个:

    final Query habitDataCollection = Firestore.instance.collection('habits').where("habitFollowers", arrayContains: 'userID');
    

    【讨论】:

    • 感谢您的快速响应!这种方式会破坏我的流提供者: Stream> get habitData { return habitDataCollection.snapshots() .map(_habitDataListFromSnapshot); } 抱歉,刚刚进入 Flutter 并试图理解它。
    • 到底是什么问题? habitDataCollection 返回一个 Query 然后你可以调用 snapshots() 也可以调用 github.com/FirebaseExtended/flutterfire/blob/master/packages/… 这将返回一个 QuerySnapshot 类型的 Stream
    • 我很抱歉 - 你是对的。我一定有一些我没有看到的错误。你描述的一切都很好。非常感谢!!
    • 完美!在我的特定用例中,我现在可以查询子集合,而不必在 Stream 中有父 documentId。
    猜你喜欢
    • 2020-12-25
    • 1970-01-01
    • 2021-07-28
    • 2022-06-16
    • 2020-10-28
    • 1970-01-01
    • 2022-11-18
    • 2023-01-26
    • 2021-12-15
    相关资源
    最近更新 更多