'Can use provider in init state (but it gives me an error)
I can use the provider in the init state, but it does give me an error. I saw answers that you need to use listen: false
in the provider. of
method but it still gives me this block of errors. Do you have any idea?
Init state code
@override
void initState() {
// TODO: implement initState
super.initState();
final paymentViewModel =
Provider.of<PaymentViewModel>(context, listen: false);
// paymentViewModel.getPaymentStatus(cartViewModel);
paymentViewModel.getPaymentStatus();
}
Error Message
══╡ EXCEPTION CAUGHT BY FOUNDATION LIBRARY ╞════════════════════════════════════════════════════════
The following assertion was thrown while dispatching notifications for PaymentViewModel:
setState() or markNeedsBuild() called during build.
This _InheritedProviderScope<PaymentViewModel?> widget cannot be marked as needing to build because
the framework is already in the process of building widgets. A widget can be marked as needing to be
built during the build phase only if one of its ancestors is currently building. This exception is
allowed because the framework builds parent widgets before children, which means a dirty descendant
will always be built. Otherwise, the framework might not visit this widget during this build phase.
The widget on which setState() or markNeedsBuild() was called was:
_InheritedProviderScope<PaymentViewModel?>
The widget which was currently being built when the offending call was made was:
Builder
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|