fluro icon indicating copy to clipboard operation
fluro copied to clipboard

Can't create ChangeNotifierProvider from handlerFunc

Open tomasweigenast opened this issue 2 years ago • 0 comments

I want to create a ChangeNotifierProvider with passed values from route arguments 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(),
    )));

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):

tomasweigenast avatar Jul 27 '21 15:07 tomasweigenast