0

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

General Grievance
  • 4,555
  • 31
  • 31
  • 45

1 Answers1

0

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()
    ),
  ),
);
Babul
  • 1,076
  • 7
  • 9
  • this really worked, thanks. But why this happens?, and if there is a situation where i need to pass the provider through ScreenA pushed to ScreeB, and only wrap the ScreenA ? is there a good solution, or i have to pass it by the constructors? – caio melloni Dec 29 '21 at 17:12