mvasco
mvasco

Reputation: 5101

Adding multiple providers to root widget

This is the root widget from my Flutter app:

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')],
    ),
  );
}

I have instantiate ClinicaProvider(), but now I need to add more providers, for example UsersProvider(), what is the best way to do it?

Upvotes: 1

Views: 207

Answers (1)

Marcel Dz
Marcel Dz

Reputation: 2714

You can work with MultiProviders

example:

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

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

Upvotes: 2

Related Questions