Combine two Streams into one StreamProvider

E

EzPizza

Guest
I have two streams:

  • Stream<FirebaseUser> FirebaseAuth.instance.onAuthStateChanged
  • Stream<User> userService.streamUser(String uid)

My userService requires the uid of the authenticated FirebaseUser as a parameter.

Since I will probably need to access the streamUser() stream in multiple parts of my app, I would like it to be a provider at the root of my project.

This is what my main.dart looks like:

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
Widget build(BuildContext context) {
var auth = FirebaseAuth.instance;
var userService = new UserService();
return MultiProvider(
providers: [
Provider<UserService>.value(
value: userService,
),
],
child: MaterialApp(
home: StreamBuilder<FirebaseUser>(
stream: auth.onAuthStateChanged,
builder: (context, snapshot) {
if (!snapshot.hasData) return LoginPage();
return StreamProvider<User>.value(
value: userService.streamUser(snapshot.data.uid),
child: HomePage(),
);
}),
),
);
}
}


The issue is that when I navigate to a different page, everything below the MaterialApp is changed out and I lose the context with the StreamProvider.

Is there a way to add the StreamProvider to the MultiProvider providers-list? Because when I try, I also have to create another onAuthStateChanged stream for the FirebaseUser and I don't know how to combine them into one Provider.

Continue reading...
 
Top