Flutter Provider - StreamProvider 依赖于另一个 StreamProvider
tldr 如何使用来自一个流提供商的数据来调用另一个流提供者?
问题的背景
你好!我有这个firebase实时数据库结构
每个用户的组ID都存储在此处。可以访问它们,然后查询以下“组” JSON树。
这里有一个组的所有数据,多个用户可以访问这些数据。这样的结构是为了避免数据冗余。
问题
我正在使用提供商软件包。为了获取用户数据,它就像魅力一样工作:
- 我使用流提供商
- ,请查询具有onValue函数的数据库-E>溪流。
- 我将数据从该流映射到一个类,并使用FromMap()工厂功能将其转换为该类的结构,
Stream<FrediUser> currentUserDataStream() {
var userRef =
FirebaseDatabase.instance.reference().child('Users').
child(currentUserID!);
return userRef.onValue.map((event) =>
FrediUser.fromMap(event.snapshot.value));
}
如果我必须访问用户映射及其嵌套的孩子,该功能有效。 但是如上所述,如果我必须先访问用户映射,然后组映射会发生什么?我想使用流提供程序来完成所有这些操作。
TLDR
How to use data from one stream provider to call another?
Background of the Problem
Hello! I have this Firebase Realtime Database Structure
Each user's Group IDs are stored here. They can be accessed to then query the following 'Groups' Json tree.
Here there is all the data for a group, which multiple users can access. The structure is like this to avoid data redundancy.
Problem
I am using the Provider package. To get the user data, it works like a charm:
- I use a Stream Provider
- I query the database with an onValue function --> Stream.
- I map the data from that Stream to a class and transform it to that class's structure using fromMap() factory function
Stream<FrediUser> currentUserDataStream() {
var userRef =
FirebaseDatabase.instance.reference().child('Users').
child(currentUserID!);
return userRef.onValue.map((event) =>
FrediUser.fromMap(event.snapshot.value));
}
That works if I have to access the User map and its nested children. But as illustrated in the example above, what happens if I have to access the User map first and then the Groups map? I want to do all of this using stream providers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过使用第一个流作为第二个流的依赖项来完成此操作,如下所示
you can do it by using the first stream as dependency of the second like below