Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MaterialApp's builder function breaks provider. Home: does not #825

Closed
feyokorenhof opened this issue Jul 6, 2023 · 5 comments
Closed
Assignees
Labels
bug Something isn't working question Further information is requested

Comments

@feyokorenhof
Copy link

feyokorenhof commented Jul 6, 2023

Describe the bug

I have to use MaterialApp's builder function instead of home to ensure MediaQuery applies to nested routes. But for some reason this breaks provider and gives me a lot of ProviderNotFoundException. I am unsure if this is a bug in Provider, Flutter or a mistake I'm doing but I've tried a lot of variations with placing my MultiProvider above and below MaterialApp and placing widgets in-between them but nothing seems to work.

EDIT:
it looks like the errors only show up when I use Navigator.push() I really don't understand why this would be the case when using builder instead of home.

To Reproduce

use MaterialApp's builder function instead of home parameter.

This does not crash:

MultiProvider(
    providers: [
        ChangeNotifierProvider(create: (_) => CarouselProvider()),
         ...
    ],
    child:
            MaterialApp(
                  home:
    Consumer<CarouselProvider>(builder: (context, csp, child) => ANestedWidget(context));
                  ));

This does:

MultiProvider(
    providers: [
        ChangeNotifierProvider(create: (_) => CarouselProvider()),
         ...
    ],
    child: MaterialApp(
                  builder: (context, child) {
                       // Nested widgets inside this consumer give a `ProviderNotFoundException`. Consumer itself does not.
                       return Consumer<CarouselProvider>(builder: (context, csp, child) => ANestedWidget(context));
                  }));

Expected behavior
For provider to still work when using the builder function.

I'm sorry if this is a silly mistake or not a provider bug but I'm out of ideas. Any help is appreciated!!

@rrousselGit
Copy link
Owner

I don't understand your "This does not crash" code. There's no home parameter on MultiProvider

@rrousselGit rrousselGit added question Further information is requested and removed needs triage labels Jul 6, 2023
@feyokorenhof
Copy link
Author

Ah, my bad I messed that up. Edited the code.

It's about the MaterialApp builder and home function.

@rrousselGit
Copy link
Owner

I'm not aware of a specific reason why that would happen.
Could you make a complete reproducible example? Something executable that's also short

@feyokorenhof
Copy link
Author

I'm going down quite the rabbit hole atm :p. It looks like It might actually be a problem of using the context used by pushing a new MaterialPageRoute with Navigator.of(context).push(). I'll do some more testing and come back to you.

Thanks for the quick responses!

@feyokorenhof
Copy link
Author

We managed to fix our problem without using builder so this is no longer an issue for us. I'm still curious as to how this behavior happened to us so I might come back to it later just for fun.

But we can close this issue afaic :).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants