Reputation: 1164
TLDR How to use data from one stream provider to call another?
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.
I am using the Provider package. To get the user data, it works like a charm:
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.
Upvotes: 4
Views: 1292
Reputation: 937
you can do it by using the first stream as dependency of the second like below
StreamProvider<FrediUser>(
create: (context) => firstStream(),
initialData: 'initialData',
builder: (context, child) {
return StreamProvider(
create: (context) => secondStream(
context.watch<FrediUser>(),
),
initialData: 'initialData',
child: child,
);
},
)
Upvotes: 2