Search code examples
flutterdartflutter-providerstate-management

How to properly reuse a Provider in Flutter when i push another screen


I have the exact same problem as it was described in this question , but i would like to reuse the provider in a pushed screen. For example: if i have the ChangeNotifierProvider wrapping the screenA, and then i push from screenA to screenB using Navigator.of(context).push..., i would like to acess the provider on the screenB without having to pass it as an argument. I am not able to do it as the answer said on the last question


Solution

  • Solution: move the provider above the MaterialApp, after that you can access the provider from screenA, B, C or etc any where from application without having to pass it as an argument

    void main() => runApp(
      ChangeNotifierProvider(
        create: (_) => YourProvider(),
        child: MaterialApp(
          debugShowCheckedModeBanner: false,
          theme: ThemeData.light(),
          home: Home()
        ),
      ),
    );