向根小部件添加多个提供商



这是我的Flutter应用程序的根小部件:

Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
SharedPreferences prefs = await SharedPreferences.getInstance();
var email = prefs.getString('email');
print(email);
runApp(
EasyLocalization(
child: ChangeNotifierProvider(
create: (BuildContext context) => ClinicaProvider(),
child: MaterialApp(
debugShowCheckedModeBanner: false,
home: email == null || email == '' ? Splash2() : HomeScreen())),
path: "assets/translations",
saveLocale: true,
supportedLocales: [Locale('en', 'EN'), Locale('es', 'ES')],
),
);
}

我已经实例化了ClinicaProvider(),但是现在我需要添加更多的提供者,例如UsersProvider(),最好的方法是什么?

您可以使用MultiProviders

的例子:

class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
ChangeNotifierProvider(create: (context) => SwipePlayerControls()),
ChangeNotifierProvider(
create: (context) => SongNotifier()),
],
child: MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
accentColor: Color.fromRGBO(236, 36, 105, 1),
canvasColor: Color.fromRGBO(15, 15, 16, 1),
),
home: MyHomePage(),
),
);
}
}

来源:https://medium.com/codespace69/flutter-how-does-multiprovider-work-with-providers-of-the-same-type-bd632bd2cbad

相关内容

最新更新