flutter_riverpod package issue with provider
Opened this issue · 1 comments
Hi debasmitasarkar,
Thanks for having let us be able to use your code and edit it :)
Nevertheless I encounter an issue that might come from flutter_riverpod.
Please see the errors that I have down there:
E/flutter (24935): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Tried to modify a provider while the widget tree was building. E/flutter (24935): If you are encountering this error, chances are you tried to modify a provider E/flutter (24935): in a widget life-cycle, such as but not limited to: E/flutter (24935): - build E/flutter (24935): - initState E/flutter (24935): - dispose E/flutter (24935): - didUpdateWidget E/flutter (24935): - didChangeDepedencies E/flutter (24935): E/flutter (24935): Modifying a provider inside those life-cycles is not allowed, as it could E/flutter (24935): lead to an inconsistent UI state. For example, two widgets could listen to the E/flutter (24935): same provider, but incorrectly receive different states. E/flutter (24935): E/flutter (24935): E/flutter (24935): To fix this problem, you have one of two solutions: E/flutter (24935): - (preferred) Move the logic for modifying your provider outside of a widget E/flutter (24935): life-cycle. For example, maybe you could update your provider inside a button's E/flutter (24935): onPressed instead. E/flutter (24935): E/flutter (24935): - Delay your modification, such as by encasuplating the modification E/flutter (24935): in a
Future(() {...}). E/flutter (24935): This will perform your update after the widget tree is done building. E/flutter (24935): #0 _UncontrolledProviderScopeElement._debugCanModifyProviders (package:flutter_riverpod/src/framework.dart:343:7) E/flutter (24935): #1 ProviderElementBase._notifyListeners.<anonymous closure> (package:riverpod/src/framework/element.dart:480:34) E/flutter (24935): #2 ProviderElementBase._notifyListeners (package:riverpod/src/framework/element.dart:482:8) E/flutter (24935): #3 ProviderElementBase.setState (package:riverpod/src/framework/element.dart:150:7) E/flutter (24935): #4 StateProviderElement.create.<anonymous closure> (package:riverpod/src/state_provider/base.dart:141:9) E/flutter (24935): #5 StateNotifier.state= (package:state_notifier/state_notifier.dart:225:31) E/flutter (24935): #6 StateController.state= (package:riverpod/src/state_controller.dart:15:31) E/flutter (24935): #7 HomeScreenController.getFavourites (package:pokedex/presentation/pages/home/home_screen_controller.dart:105:48) E/flutter (24935): #8 _HomeScreenState.initState (package:pokedex/presentation/pages/home/home_screen.dart:21:44) E/flutter (24935): #9 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:5101:55) E/flutter (24935): #10 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4944:5) E/flutter (24935): #11 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3953:16) E/flutter (24935): #12 Element.updateChild (package:flutter/src/widgets/framework.dart:3682:18) E/flutter (24935): #13 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6377:14) E/flutter (24935): #14 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3953:16) E/flutter (24935): #15 Element.updateChild (package:flutter/src/widgets/framework.dart:3682:18) E/flutter (24935): #16 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4993:16) E/flutter (24935): #17 Element.rebuild (package:flutter/src/widgets/framework.dart:4690:5) E/flutter (24935): #18 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4950:5) E/flutter (24935): #19 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4944:5) E/flutter (24935): #20 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3953:16) E/flutter (24935): #21 Element.updateChild (package:flutter/src/widgets/framework.dart:3682:18) E/flutter (24935): #22 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6377:14) E/flutter (24935): #23 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3953:16) E/flutter (24935): #24 Element.updateChild (package:flutter/src/widgets/framework.dart:3682:18) E/flutter (24935): #25 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6377:14) E/flutter (24935): #26 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3953:16) E/flutter (24935): #27 Element.updateChild (package:flutter/src/widgets/framework.dart:3682:18) E/flutter (24935): #28 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4993:16) E/flutter (24935): #29 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:5133:11) E/flutter (24935): #30 Element.rebuild (package:flutter/src/widgets/framework.dart:4690:5) E/flutter (24935): #31 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4950:5) E/flutter (24935): #32 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:5124:11) E/flutter (24935): #33 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4944:5) E/flutter (24935): #34 Element.inflateWidget (package:flu E/flutter (24935): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Tried to modify a provider while the widget tree was building. E/flutter (24935): If you are encountering this error, chances are you tried to modify a provider E/flutter (24935): in a widget life-cycle, such as but not limited to: E/flutter (24935): - build E/flutter (24935): - initState E/flutter (24935): - dispose E/flutter (24935): - didUpdateWidget E/flutter (24935): - didChangeDepedencies E/flutter (24935): E/flutter (24935): Modifying a provider inside those life-cycles is not allowed, as it could E/flutter (24935): lead to an inconsistent UI state. For example, two widgets could listen to the E/flutter (24935): same provider, but incorrectly receive different states. E/flutter (24935): E/flutter (24935): E/flutter (24935): To fix this problem, you have one of two solutions: E/flutter (24935): - (preferred) Move the logic for modifying your provider outside of a widget E/flutter (24935): life-cycle. For example, maybe you could update your provider inside a button's E/flutter (24935): onPressed instead. E/flutter (24935): E/flutter (24935): - Delay your modification, such as by encasuplating the modification E/flutter (24935): in a
Future(() {...}). E/flutter (24935): This will perform your update after the widget tree is done building. E/flutter (24935): #0 _UncontrolledProviderScopeElement._debugCanModifyProviders (package:flutter_riverpod/src/framework.dart:343:7) E/flutter (24935): #1 ProviderElementBase._notifyListeners.<anonymous closure> (package:riverpod/src/framework/element.dart:480:34) E/flutter (24935): #2 ProviderElementBase._notifyListeners (package:riverpod/src/framework/element.dart:482:8) E/flutter (24935): #3 ProviderElementBase.setState (package:riverpod/src/framework/element.dart:150:7) E/flutter (24935): #4 StateProviderElement.create.<anonymous closure> (package:riverpod/src/state_provider/base.dart:141:9) E/flutter (24935): #5 StateNotifier.state= (package:state_notifier/state_notifier.dart:225:31) E/flutter (24935): #6 StateController.state= (package:riverpod/src/state_controller.dart:15:31) E/flutter (24935): #7 HomeScreenController.getPokemons (package:pokedex/presentation/pages/home/home_screen_controller.dart:64:44) E/flutter (24935): #8 _AllPokemonWidgetState._fetchPage (package:pokedex/presentation/widgets/all_pokemon_widget.dart:36:56) E/flutter (24935): #9 _AllPokemonWidgetState.init.<anonymous closure> (package:pokedex/presentation/widgets/all_pokemon_widget.dart:58:15) E/flutter (24935): #10 PagingController.notifyPageRequestListeners.<anonymous closure> (package:infinite_scroll_pagination/src/core/paging_controller.dart:214:17) E/flutter (24935): #11 List.forEach (dart:core-patch/growable_array.dart:416:8) E/flutter (24935): #12 PagingController.notifyPageRequestListeners (package:infinite_scroll_pagination/src/core/paging_controller.dart:212:20) E/flutter (24935): #13 _PagedSliverBuilderState.build.<anonymous closure> (package:infinite_scroll_pagination/src/ui/paged_sliver_builder.dart:147:31) E/flutter (24935): #14 _ListenableListenerState._handleChange (package:infinite_scroll_pagination/src/utils/listenable_listener.dart:53:22) E/flutter (24935): #15 _ListenableListenerState.initState (package:infinite_scroll_pagination/src/utils/listenable_listener.dart:34:5) E/flutter (24935): #16 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:5101:55) E/flutter (24935): #17 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4944:5) E/flutter (24935): #18 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3953:16) E/flutter (24935): #19 Element.updateChild (package:flutter/src/widgets/framework.dart:3682:18) E/flutter (24935): #20 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4993:16) E/flutter (24935): #21 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:5133:11) E/flutter (24935): #22 Element.rebuild (package:flutter/src/widgets/framework.dart:4690:5) E/flutter (24935): #23 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4950:5) E/flutter (24935): #24 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:5124:11) E/flutter (24935): #25 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4944:5) E/flutter (24935): #26 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3953:16) E/flutter (24935): #27 Element.updateChild (package:flutter/src/widgets/framework.dart:3682:18) E/flutter (24935): #28 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4993:16) E/flutter (24935): #29 Element.rebuild (package:flutter/src/widgets/framework.dart:4690:5) E/flutter (24935): #30 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4950:5) E/flutter (24935): #31 ComponentElement.mount (package:flutter/src/widg E/flutter (24935): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: At least listener of the StateNotifier Instance of 'StateController<List<PokemonDetailEntity>>' threw an exception E/flutter (24935): when the notifier tried to update its state. E/flutter (24935): E/flutter (24935): The exceptions thrown are: E/flutter (24935): E/flutter (24935): Tried to modify a provider while the widget tree was building. E/flutter (24935): If you are encountering this error, chances are you tried to modify a provider E/flutter (24935): in a widget life-cycle, such as but not limited to: E/flutter (24935): - build E/flutter (24935): - initState E/flutter (24935): - dispose E/flutter (24935): - didUpdateWidget E/flutter (24935): - didChangeDepedencies E/flutter (24935): E/flutter (24935): Modifying a provider inside those life-cycles is not allowed, as it could E/flutter (24935): lead to an inconsistent UI state. For example, two widgets could listen to the E/flutter (24935): same provider, but incorrectly receive different states. E/flutter (24935): E/flutter (24935): E/flutter (24935): To fix this problem, you have one of two solutions: E/flutter (24935): - (preferred) Move the logic for modifying your provider outside of a widget E/flutter (24935): life-cycle. For example, maybe you could update your provider inside a button's E/flutter (24935): onPressed instead. E/flutter (24935): E/flutter (24935): - Delay your modification, such as by encasuplating the modification E/flutter (24935): in a
Future(() {...}). E/flutter (24935): This will perform your update after the widget tree is done building. E/flutter (24935): #0 _UncontrolledProviderScopeElement._debugCanModifyProviders (package:flutter_riverpod/src/framework.dart:343:7) E/flutter (24935): #1 ProviderElementBase._notifyListeners.<anonymous closure> (package:riverpod/src/framework/element.dart:480:34) E/flutter (24935): #2 ProviderElementBase._notifyListeners (package:riverpod/src/framework/element.dart:482:8) E/flutter (24935): #3 ProviderElementBase.setState (package:riverpod/src/framework/element.dart:150:7) E/flutter (24935): #4 StateProviderElement.create.<anonymous closure> (package:riverpod/src/state_provider/base.dart:141:9) E/flutter (24935): #5 StateNotifier.state= (package:state_notifier/state_notifier.dart:225:31) E/flutter (24935): #6 StateController.state= (package:riverpod/src/state_controller.dart:15:31) E/flutter (24935): #7 HomeScreenController.getFavourites (package:pokedex/presentation/pages/home/home_screen_controller.dart:105:48) E/flutter (24935): #8 _HomeScreenState.initState (package:pokedex/presentation/pages/home/home_screen.dart:21:44) E/flutter (24935): #9 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:5101:55) E/flutter (24935): #10 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4944:5) E/flutter (24935): #11 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3953:16) E/flutter (24935): #12 Element.updateChild (package:flutter/src/widgets/framework.dart:3682:18) E/flutter (24935): #13 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6377:14) E/flutter (24935): #14 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3953:16) E/flutter (24935): #15 Element.updateChild (package:flutter/src/widgets/framework.dart:3682:18) E/flutter (24935): #16 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4993:16) E/flutter (24935): #17 Element.rebuild (package:flutter/src/widgets/framework.dart:4690:5) E/flutter (24935): #18 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4950:5) E/flutter (24935): #19 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4944:5) E/flutter (24935): #20 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3953:16) E/flutter (24935): #21 Element.updateChild (package:flutter/src/widgets/framework.dart:3682:18) E/flutter (24935): #22 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6377:14) E/flutter (24935): #23 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3953:16) E/flutter (24935): #24 Element.updateChild (package:flutter/src/widgets/framework.dart:3682:18) E/flutter (24935): #25 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6377:14) E/flutter (24935): #26 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3953:16) E/flutter (24935): #27 Element.updateChild (package:flutter/src/widgets/framework.dart:3682:18) E/flutter (24935): #28 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4993:16) E/flutter (24935): #29 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:5133:11) E/flutter (24935): #30 Element.rebuild (package:flutter/src/widgets/framework.dart:4690:5) E/flutter (24935): #31 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4950:5) E/flutter (24935): #32 StatefulElement._
@BadisB Thanks for pointing out. Will take a look.