在Runapp上使用两个更改通知器进行颤振

时间:2020-02-03 20:22:58

标签: flutter main

我有此更改通知者:

runApp(
  ChangeNotifierProvider<AuthService>(
    child: MyApp(),
    create: (BuildContext context) {
      return AuthService();
    },
  ),
);

但是我还需要添加一个新的:

ChangeNotifierProvider(
  create: (_) => ThemeProvider(isLightTheme: true),
    child: MyApp(),
);

由于runapp似乎不允许这样做,我该怎么办?

1 个答案:

答案 0 :(得分:1)

您可以嵌套提供程序或使用MultiProvider

ChangeNotifierProvider(
  create: (_) => AuthService(),
  child: ChangeNotifierProvider(
    create: (_) => ThemeProvider(),
    child: MyApp(),
  ),
)

或:

MultiProvider(
  providers: [
    ChangeNotifierProvider(create: (_) => AuthService()),
    ChangeNotifierProvider(create: (_) => ThemeProvider()),
  ],
  child: MyApp(),
)