Flutter Firestore和Flatter:使用流优化数据读取 解释

Flutter Firestore和Flatter:使用流优化数据读取 解释,flutter,google-cloud-firestore,stream,bloc,Flutter,Google Cloud Firestore,Stream,Bloc,我使用BLoC逻辑来监听我的流,我希望最小化数据读取 我知道QuerySnapshot会跟踪以前的文档,并在文档未更改时从缓存中拾取 问题是快照不包含有关用户的配置文件图片、名称等的信息,我必须使用getInfoFromId()获取这些信息,我希望找到一种方法,在一个用户被更改、删除或添加时,不要对每个用户使用此函数 Stream<List<FeedUser>> rolesToStream(String newsId) { return Firestore.insta

我使用BLoC逻辑来监听我的流,我希望最小化数据读取

我知道QuerySnapshot会跟踪以前的文档,并在文档未更改时从缓存中拾取

问题是快照不包含有关用户的配置文件图片、名称等的信息,我必须使用getInfoFromId()获取这些信息,我希望找到一种方法,在一个用户被更改、删除或添加时,不要对每个用户使用此函数

  Stream<List<FeedUser>> rolesToStream(String newsId) {
return Firestore.instance
    .collection('pages')
    .document(newsId).collection('users')
    .snapshots()
    .map(
        (snapshot){
      return snapshot.documents.map(
              (document){
            FeedUser user = FeedUser();
            user.fromSnapshot(document);
            user.contact = getInfoFromId(user.id);
            // The contact infos of a user is a future because we have to get it elsewhere, in a Firebase (NOT FIRESTORE) database. It theorically shouldn't be read after being read once after the event "RoleLoadMembers" 
            // Then we retrieve the contact infos after it is retrieved
            user.contact.then(
                    (contact){
                  user.retrievedContact = contact;
                }
            );
            return user;
          }
      ).toList();
    }
);
}
我的问题是:每当我改变一个用户的角色时,每个其他用户都会被流重新读取,我不知道如何修复它。 提前谢谢你的帮助


状态日志 可能状态:

  • RoleLoadingMembers
  • RoleLoadingSuccess
  • RoleLoadingFailure(从未发生过)
可能的事件:

  • RoleLoadMembers
  • 角色更新
  • 换角色
  • 罢免成员
  • 添加成员
这是第一次加载后的日志:

I/flutter (27792): RoleLoadMembers
I/flutter (27792): Reading infos of the user : E4nT23Ohi0Za7JIQDaQ7Ohwe7Rn1
I/flutter (27792): Reading infos of the user : Svbj4tAIhIRcjQNqvYYsLSDXxwu2
I/flutter (27792): Reading infos of the user : cBm2KG6rEAbiaLGMAC08bvefNSn1
I/flutter (27792): RoleUpdated
I/flutter (27792): Transition { currentState: RoleMembersLoading, event: RoleUpdated, nextState: RoleLoadingSuccess }
角色转换后:

I/flutter (27792): ChangeRole
I/flutter (27792): Reading infos of the user : E4nT23Ohi0Za7JIQDaQ7Ohwe7Rn1
I/flutter (27792): Reading infos of the user : Svbj4tAIhIRcjQNqvYYsLSDXxwu2
I/flutter (27792): Reading infos of the user : cBm2KG6rEAbiaLGMAC08bvefNSn1
I/flutter (27792): RoleUpdated
I/flutter (27792): Transition { currentState: RoleLoadingSuccess, event: RoleUpdated, nextState: RoleLoadingSuccess }
删除成员后(已正确删除成员,但仍读取数据库中的信息):


如果要在查询服务器之前先从客户端本地缓存中读取,可以这样做以避免读取。只需指定一个查询缓存,然后如果缓存不存在,则查询服务器。阅读更多信息。

如果要在查询服务器之前先从客户端本地缓存中读取,可以这样做以避免读取。只需指定一个查询缓存,然后如果缓存不存在,则查询服务器。阅读更多信息。

如果您不想重读已经阅读过的文档,您可以实现自己的客户端缓存。我将此作为最后手段,因为我从来没有这样做过,但我将尝试一下,学习不会有任何伤害:)!祝你度过愉快的一天如果你不想重读你已经阅读过的文档,听起来你可以实现自己的客户端缓存。我把这当作最后的手段,因为我从来没有这样做过,但我要尝试一下,学习不会有任何伤害:)!祝你今天愉快
I/flutter (27792): RoleLoadMembers
I/flutter (27792): Reading infos of the user : E4nT23Ohi0Za7JIQDaQ7Ohwe7Rn1
I/flutter (27792): Reading infos of the user : Svbj4tAIhIRcjQNqvYYsLSDXxwu2
I/flutter (27792): Reading infos of the user : cBm2KG6rEAbiaLGMAC08bvefNSn1
I/flutter (27792): RoleUpdated
I/flutter (27792): Transition { currentState: RoleMembersLoading, event: RoleUpdated, nextState: RoleLoadingSuccess }
I/flutter (27792): ChangeRole
I/flutter (27792): Reading infos of the user : E4nT23Ohi0Za7JIQDaQ7Ohwe7Rn1
I/flutter (27792): Reading infos of the user : Svbj4tAIhIRcjQNqvYYsLSDXxwu2
I/flutter (27792): Reading infos of the user : cBm2KG6rEAbiaLGMAC08bvefNSn1
I/flutter (27792): RoleUpdated
I/flutter (27792): Transition { currentState: RoleLoadingSuccess, event: RoleUpdated, nextState: RoleLoadingSuccess }
I/flutter (27792): RemoveMember
I/flutter (27792): Reading infos of the user : E4nT23Ohi0Za7JIQDaQ7Ohwe7Rn1
I/flutter (27792): Reading infos of the user : Svbj4tAIhIRcjQNqvYYsLSDXxwu2
I/flutter (27792): RoleUpdated
I/flutter (27792): Transition { currentState: RoleLoadingSuccess, event: RoleUpdated, nextState: RoleLoadingSuccess }