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

Initialize provider with constructor arguments #662

Open
tomasweigenast opened this issue Jul 27, 2021 · 1 comment
Open

Initialize provider with constructor arguments #662

tomasweigenast opened this issue Jul 27, 2021 · 1 comment
Assignees
Labels
enhancement New feature or request

Comments

@tomasweigenast
Copy link

I want to create a ChangeNotifierProvider with passed values from route arguments (im using Fluro for routing) like this:

_fluroRouter.define(BusinessListPage.route, handler: Handler(handlerFunc: (context, args) => ChangeNotifierProvider(
      create: (context) {
        var category = context.settings?.arguments as CommercialCategory?;
        return BusinessListViewModel(commercialCategory: category!);
      },
      builder: (context, child) => BusinessListPage(),
    )));

BusinessListPage builds a Consumer<BusinessListViewModel>

But i throws the following exception when I try to navigate to that route

I/flutter (19739): [FLUTTER ERROR] ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞════════════════════════
I/flutter (19739): The following assertion was thrown building
I/flutter (19739): Consumer<BusinessListViewModel>(dirty, dependencies:
I/flutter (19739): [_InheritedProviderScope<BusinessListViewModel>]):
I/flutter (19739): Tried to listen to an InheritedWidget in a life-cycle that will
I/flutter (19739): never be called again.
I/flutter (19739): This error typically happens when calling Provider.of with
I/flutter (19739): `listen` to `true`,
I/flutter (19739): in a situation where listening to the provider doesn't make
I/flutter (19739): sense, such as:
I/flutter (19739): - initState of a StatefulWidget
I/flutter (19739): - the "create" callback of a provider
I/flutter (19739):
I/flutter (19739): This is undesired because these life-cycles are called only once
I/flutter (19739): in the
I/flutter (19739): lifetime of a widget. As such, while `listen` is `true`, the
I/flutter (19739): widget has
I/flutter (19739): no mean to handle the update scenario.
I/flutter (19739):
I/flutter (19739): To fix, consider:
I/flutter (19739): - passing `listen: false` to `Provider.of`
I/flutter (19739): - use a life-cycle that handles updates (like
I/flutter (19739): didChangeDependencies)
I/flutter (19739): - use a provider that handles updates (like ProxyProvider).
I/flutter (19739):
I/flutter (19739): The relevant error-causing widget was:
I/flutter (19739):   Consumer<BusinessListViewModel>
I/flutter (19739):
lib\…\search\business_list_page.dart:43
I/flutter (19739):

What am I missing?

@tomasweigenast tomasweigenast added enhancement New feature or request needs triage labels Jul 27, 2021
@rrousselGit
Copy link
Owner

Provider does not support accessing InheritedWidgets inside create at the moment.

@rrousselGit rrousselGit self-assigned this May 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants