简体   繁体   English

flutter firestore 我如何过滤数据

[英]flutter firestore how I filter data

I have a collection (Groups) inside it fields contain information about users who joined the group,我在其中有一个集合(组)字段包含有关加入该组的用户的信息,

I want to display to the user all groups he joined.我想向用户显示他加入的所有组。

List groupList = [];

void getAvailableGroups() async {
    await _fireStore
        .collection('Groups')
        .get()
        .then((value) {
        groupList = value.docs;
    });
  }

收藏

I tried to convert from map into array but it gives me array within map this is my code我试图从 map 转换为数组,但它在 map 内给了我数组,这是我的代码

Future createGroup() async{
    GroupRoomModel newGroup = GroupRoomModel(
        groupName: groupName.text,
        groupRoomId: uuid.v1(),
        owner: userModel.uid,
        membersList: controller.membersList,
        membersListUid: controller.membersListUid.cast() // like this
    );
}
...

Also I tried我也试过

  Future createGroupFunc() async{
    GroupRoomModel newGroup = GroupRoomModel(
        groupName: groupName.text,
        groupRoomId: uuid.v1(),
        owner: userModel.uid,
        membersList: controller.membersList,
        membersListUid: controller.membersListUid.map((e)=> e).toList()
    );
...

It might be tempting to try filtering based on something like this:尝试基于以下内容进行过滤可能很诱人:

_fireStore
  .collection('Groups')
  .where('membersList', arrayContains: 'test@email.com')

This won't work though, as arrayContains only finds something a match when it matches a complete item in the array.但这行不通,因为arrayContains仅在匹配数组中的完整项目时才找到匹配项。 You can't use arrayContains to match just a subset of an item.您不能使用arrayContains来匹配项目的一个子集。

The common solution is to add an additional array field to your documents with just the property you want to be able to query on.常见的解决方案是在您的文档中添加一个额外的数组字段,其中仅包含您希望能够查询的属性。 For example:例如:

memberEmails: ['test@email.com', 'test@example.com']

With such an addition field, you can query with:有了这样的附加字段,您可以查询:

_fireStore
  .collection('Groups')
  .where('memberEmails', arrayContains: 'test@email.com')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM