Home > Software engineering >  Properly access ChangeNotifierProvider from Consumer Flutter
Properly access ChangeNotifierProvider from Consumer Flutter

Time:04-10

I can't access my ChangeNotifierProvider<QuestionProvider> from Consumer<QuestionProvider> but I don't understand why, it's defined deeper in the widget tree compared to its provider.

I defined my provider here :

body: GestureDetector(
        onTap: () => FocusManager.instance.primaryFocus?.unfocus(),
        child: ChangeNotifierProvider<QuestionProvider>(
            //TODO : question provider arguments should come from internet
            create: (newContext) => QuestionProvider([], pageController),
            child: Column(
              children: [
                TypeAndSettingsRow(),

Consumer is defined in a next widget defined inside TypeAndSettingsRow :

class TypeAndSettingsRow extends StatefulWidget {
  @override
  State<TypeAndSettingsRow> createState() => _TypeAndSettingsRowState();
}

class _TypeAndSettingsRowState extends State<TypeAndSettingsRow> {
  @override
  Widget build(BuildContext context) {

    void showPopoverType() {
      int thisIsTrue = 0;
      double size = MediaQuery.of(context).size.width * 0.9;
      showPopover(
        context: context,
        tion(milliseconds: 150),
        bodyBuilder: (context) => CheckboxListTileRow(),

And finally CheckboxListTileRow :

class _CheckboxListTileRowState extends State<CheckboxListTileRow> {
  @override
  Widget build(BuildContext context) {
    return Consumer<QuestionProvider>(builder: ((context, value, child) {

QuestionProvider :

class QuestionProvider extends ChangeNotifier {

When I open CheckboxListTileRow this is the error :

Error: Could not find the correct Provider<QuestionProvider> above this Consumer<QuestionProvider> Widget

This happens because you used a `BuildContext` that does not include the provider
of your choice. There are a few common scenarios:
 You added a new provider in your `main.dart` and performed a hot-reload.
  To fix, perform a hot-restart.

- The provider you are trying to read is in a different route.

  Providers are "scoped". So if you insert of provider inside a route, then
  other routes will not be able to access that provider.

- You used a `BuildContext` that is an ancestor of the provider you are trying to read.

  Make sure that Consumer<QuestionProvider> is under your MultiProvider/Provider<QuestionProvider>.

I don't understand which is the problem because my provider is defined above in the widget tree respect of its consumer so it should be reachable.

Maybe there are some errors with contexts?

EDIT :

Found out that the problem is showPopover form popover package, when I try to access ChangeNotifierProvider with Consumer within showPopover this error is thrown but if I do it outside then it works. Probably it creates a new route and I can't access my provider anymore. Hope this new hint can help to fix because question is still open.

CodePudding user response:

 Make sure that Consumer<QuestionProvider> is under your MultiProvider/Provider<QuestionProvider>. 

This means you have not mentioned the provider class in your main.dart file. For that, you have to use the following code.

Provider(
  create: (_) => QuestionProvider(),
  child: MyApp(),
),

If you have multiple providers you can use the following code.

MultiProvider(
 providers: [
      QuestionProvider(),
      .....
     ]

  )

CodePudding user response:

why did you use changeNotifierProvider on root of your widget tree? if you haven't need to like that you can use it where ever in your widget tree and use consumer as it's on child not another widget

  • Related