0

I'm having problems trying to obtain data from a provider. I've been working a lot with them so I know all the usual errors that you can get with, but this one is driving me crazy.

I have a simple widget that gets data from PassesSelector provider:

class PassesSelector extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    List<Passes> passes = Provider.of<PassesSelectorManager>(context).passes;

    return Column(
      mainAxisAlignment: MainAxisAlignment.start,
      children: [
        DaysSelector(
          primaryColor: Colors.green,
          title: "Select pass",
          passDays: passes,
        ),
      ],
    );
  }
}

In another widget, I create a Multiprovider (as there will be more providers needed) where I initialize the PassesSelector and in child, I add a Builder that receives a column with two texts showing some PassesSelector Provider data and the PassSelector widget:

class TicketSelectionPresenter extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        ChangeNotifierProvider<PassesSelectorManager>(
          create: (_) => PassesSelectorManager()
        ),
      ],
      child: Builder(builder: (context) {
        return Column(
          children: [
            Text(context.read<PassesSelectorManager>().taxes.toString()),
            Text(context.read<PassesSelectorManager>().dayPasses.toString()),
            PassesSelector(),
          ],
        );
      }),
    );
  }
}

The thing is that the Texts inside the Column show the information properly (so, the provider is created) but when rendering the PassesSelector() I'm getting the error Could not find the correct Provider<PassesSelectorManager> above this PassesSelector widget but I don't understand why, like the context received did not have the provider. I know the context is different if you push a route, but in this case we're just adding a Widget to the context...

Things you might want to know:

  • [√] Flutter (Channel stable, 2.10.2, on Microsoft Windows [Version 10.0.19042.1526], locale en-GB)
  • [√] VS Code (version 1.65.2)
  • [√] Android toolchain - develop for Android devices (Android SDK version 30.0.2)

Things I tried so far:

  • Using the builder(context, child) => PassesSelector() function of MultiProvider instead of child
  • Add Consumer<PassesSelector> inside the Builder column and add PassesSelector() as child
  • Add Consumer<PassesSelector> inside the build function of PassesSelector()
  • Placed all the Column with Texts and PassesSelector as a new Widget, and the same happens, the Texts can show the data properly but the widget PassesSelector() throws the error.

Any help is appreciated, thank you!

2
  • How exactly have you tried with builder? In a very similar scenario it works for me with builder. Are you sure the error is about this PassesSelectorManager? Mar 11, 2022 at 11:09
  • Hi @PeterKoltai! I've updated the "Things I tried" to let you view clearly what I tried there. Is so weird for me as well, as I have used it tons of times without errors... :'( I checked twice and yes, the error (at least what Flutter says) is with PassesSelector
    – Juli15
    Mar 11, 2022 at 11:38

2 Answers 2

0

I would suggest to try to pass the context's PassesSelectorManager provider to PassesSelector. I think you need watch and not read but I don't know what's in your PassesSelectorManager, for example are there any notifyListeners called. Try this code:

class PassesSelector extends StatelessWidget {
  final PassesSelectorManager passesSelectionManager;
  const PassesSelector({required this.passesSelectionManager, Key? key})
   : super (key: key); 
  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.start,
      children: [
        DaysSelector(
          primaryColor: Colors.green,
          title: "Select pass",
          passDays: passesSelectionManager.passes,
        ),
      ],
    );
  }
}

class TicketSelectionPresenter extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        ChangeNotifierProvider<PassesSelectorManager>(
          create: (_) => PassesSelectorManager()
        ),
      ],
      builder: (context, child) {
        final passesSelectionManager = context.watch<PassesSelectorManager>();
        return Column(
          children: [
            passesSelectionManager.taxes.toString()),
            passesSelectionManager.dayPasses.toString()),
            PassesSelector(passesSelectionManager),
          ],
        );
      }),
    );
  }
}

EDIT: another approach with ListenableProvider:

You can try to use ListenableProvider, this is not used very often but can be useful in some cases. Remove the passesSelectionManager member form PassesSelector class and constructor I suggested in the original answer, and try this:

class TicketSelectionPresenter extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        ChangeNotifierProvider<PassesSelectorManager>(
          create: (_) => PassesSelectorManager()
        ),
      ],
      builder: (context, child) {
        final passesSelectionManager = context.watch<PassesSelectorManager>();
        return Column(
          children: [
            passesSelectionManager.taxes.toString()),
            passesSelectionManager.dayPasses.toString()),
            // this is the new part
            ListenableProvider<PassesSelectorManager>.value(
              value: passesSelectionManager,
              child: PassesSelector()),
          ],
        );
      }),
    );
  }
}
3
  • Hi! Thanks for your suggestion, that's for sure a solution, the problem that I see here is that even it's solved by passing the provider as variable, the provider is not in the widget tree (although the context is the one generated by Multiprovider, is what I cannot understand right). So, If you want to do a read(), watch() or create a ProxyProvider with the PassesSelectorManager you won't be able to do it because it's not in the widget tree :(
    – Juli15
    Mar 18, 2022 at 8:19
  • I see. I will ad another idea to my answer, maybe it helps, I am not sure. Mar 18, 2022 at 9:03
  • Hi Peter! Thanks for the suggestion, after these days of working on other things and examining everything carefully, I've discovered that the error was caused by a weird thing of VSCode when importing files (I will post it now for if it can help others). Really really thanks for your help anyway!
    – Juli15
    Mar 20, 2022 at 7:34
0

We've been a few days working on other things and we got again a weird error as Flutter/Provider was not working as expected. We did big research again until we found the solution.

When importing files, Visual Studio Code was importing some words in lower case while the directory name started with Caps. So, the page that was creating the provider was instantiating it from (it's an example, notice how the Providers word is written):

import 'package:xxx/Application/Providers/pass_selector_manager.dart

while the Widget reading was trying to obtain it from:

import 'package:xxx/Application/providers/pass_selector_manager.dart

We feel silly, we've been checking each widget tree generated and all the "hard" stuff around until we found out this. It's almost a meme, but well, at the end, the lesson is to check your imports when something breaks, even if they're auto imported.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.