簡體   English   中英

通過 Flutter 了解多供應商

[英]Understanding Multiprovider with Flutter

我正在尋找如何在 flutter 上獲得多個 stream 提供商。我所擁有的是這個

 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(),

      ),
    );
  }
}

我想要的是像

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(),

      )],
    );
  }
}

基本上我想要兩個不同的 stream 供應商。 一個用於特定的一組用戶,另一個用於另一組用戶。 其中每一個都指向不同的注冊方法。 有人知道如何讓它工作嗎? 提前致謝

MultiProvider只是一種注入多個提供者的方法。

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

MyWidget中根據需求選擇需要的那個。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM