[英]Flutter Provider - StreamProvider depending on another StreamProvider
TLDR How to use data from one stream provider to call another? TLDR如何使用来自一个 stream 供应商的数据来呼叫另一个供应商?
Hello!你好! I have this Firebase Realtime Database Structure
我有这个 Firebase 实时数据库结构
Each user's Group IDs are stored here.每个用户的组 ID 都存储在这里。 They can be accessed to then query the following 'Groups' Json tree.
可以访问它们以查询以下“组”Json 树。
Here there is all the data for a group, which multiple users can access.这里有一个组的所有数据,多个用户可以访问。 The structure is like this to avoid data redundancy.
这样的结构是为了避免数据冗余。
I am using the Provider package. To get the user data, it works like a charm:我正在使用提供者 package。要获取用户数据,它就像一个魅力:
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.如果我必须访问用户 map 及其嵌套的子级,那将有效。 But as illustrated in the example above, what happens if I have to access the User map first and then the Groups map?
但如上例所示,如果我必须先访问用户 map,然后再访问组 map,会发生什么情况? I want to do all of this using stream providers.
我想使用 stream 提供商来完成所有这些工作。
you can do it by using the first stream as dependency of the second like below你可以通过使用第一个 stream 作为第二个的依赖来做到这一点,如下所示
StreamProvider<FrediUser>(
create: (context) => firstStream(),
initialData: 'initialData',
builder: (context, child) {
return StreamProvider(
create: (context) => secondStream(
context.watch<FrediUser>(),
),
initialData: 'initialData',
child: child,
);
},
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.