Reputation: 171
I have an issue where I want to create a MultiProvider
with some different providers, the problem is that two of those are StreamProviders that require first to have a firebase auth login, and after that subscribe to a Stream in firebase based on the result of the user logged in.
So if I cannot launch the StreamProvider
before the login at the top of my MaterialApp
.
If I declare those providers after the login is complete I get an error that the provider is not on the correct route because I need the data in several routes around all my app.
Here is my code:
class Neybor extends StatelessWidget {
@override
Widget build(BuildContext context) {
final textTheme = GoogleFonts.nunito;
return MultiProvider(
providers: [
ChangeNotifierProvider<Data>(create: (context) => new Data()),
/// Settings Stream
StreamProvider<SettingsDataModel>.value(
value: Globals.firebaseCaller.settings(),
),
/// Plans Stream
StreamProvider<PlansDataModel>.value(
value: Globals.firebaseCaller.plans(),
),
],
child: MaterialApp(
...
}
For Globals.firebaseCaller.settings()
and Globals.firebaseCaller.plans()
I use the register user uid
Is there a way to declare a StreamProvider
and subscribe to it later on my code?
Thanks in advance
Upvotes: 4
Views: 11571
Reputation: 146
Use create
parameter in the StreamProvider
to pass your stream and subscribe to it using Provider.of<T>(context)
class Neybor extends StatelessWidget {
@override
Widget build(BuildContext context) {
final textTheme = GoogleFonts.nunito;
return MultiProvider(
providers: [
/// Settings Stream
/// Globals.firebaseCaller.settings() should returns a Stream<SettingsDataModel>
StreamProvider<SettingsDataModel>(create: (context) =>
Globals.firebaseCaller.settings(),
),
],
child: HomeView()
..
then in the HomeView()
import 'package:provider/provider.dart';
class HomeView extends StatelessWidget {
@override
Widget build(BuildContext context) {
SettingsDataModel settings = Provider.of<SettingsDataModel>(context);
if (settings == null) {
return Align(child: new CircularProgressIndicator());
} else {
// your code
...
}
...
Upvotes: 10