简体   繁体   中英

Understanding Multiprovider with Flutter

I am looking to see how I can get multiple stream providers on flutter. What I have is this

 Future main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  SystemChrome.setPreferredOrientations(
      [DeviceOrientation.portraitUp, DeviceOrientation.portraitDown]);
  runApp(const GroupVestmentApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return StreamProvider<UserInfo?>.value(
      value: AuthService().userinfo,
      initialData: null,
      child: MaterialApp(
        home: SplashScreen(),

      ),
    );
  }
}

What I want is something like

Future main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  SystemChrome.setPreferredOrientations(
      [DeviceOrientation.portraitUp, DeviceOrientation.portraitDown]);
  runApp(const GroupVestmentApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
return MultiProvider([
    child:StreamProvider<UserInfo?>.value(
      value: AuthService().userinfo,
      initialData: null,
      child: MaterialApp(
        home: SplashScreen(),

      ),
child:StreamProvider<NewInfo?>.value(
      value: AuthService().Newinfo,
      initialData: null,
      child: MaterialApp(
        home: SplashScreen(),

      )],
    );
  }
}

Basically I want to have two different stream providers. One for a certain set of users and another for another set of users. Each of these will point to a different sign up method. Does anybody know how to get this to work? Thanks in advance

MultiProvider is just a way to inject several providers.

MultiProvider(
  providers: [
    Provider<Something>(create: (_) => Something()),
    Provider<SomethingElse>(create: (_) => SomethingElse()),
    Provider<AnotherThing>(create: (_) => AnotherThing()),
  ],
  child: MyWidget(),
)

In MyWidget choose the one needed based on the requirements.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM