Reputation: 2549
I have a class which extends from ChangeNotifier, it manages the state of one widget:
MainSection _section = MainSection.SETUP;
MainSection get section => _section;
set section(MainSection value) {
_section = value;
// some code
notifyListeners();
}
As I said I use that to change the state of a Widget:
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider<MainBloc>.value(
value: _bloc,
child: Consumer<MainBloc>(builder: (context, bloc, child) {
_bloc = bloc;
var body;
switch (_bloc.section) {
case MainSection.SETUP:
body = _widgetFactory.createSetupWidget();
break;
case MainSection.WAITING:
body = Column(
children: <Widget>[
Expanded(
child: _widgetFactory.createWaitingWidget(),
),
_getBottomBar()
],
);
break;
This mechanism worked OK since I updated the app to work with the last Flutter version. Now in debug mode it works OK in all cases, but in profile or release mode it doesn't work at a specific point in the app, meaning that it's working for some state changes but for a particular change doesn't work. I don't know what can be affecting that.
Why I say it's not working: I change the variable, call notifyListeners() but the consumer doesn't get notified.
I'm using provider dependency version 4.3.1
Flutter doctor:
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, v1.17.5, on Linux, locale en_US.UTF-8)
[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
[✓] Android Studio (version 4.0)
[!] IntelliJ IDEA Community Edition (version 2019.2)
✗ Flutter plugin not installed; this adds Flutter specific functionality.
[!] VS Code (version 1.47.3)
✗ Flutter extension not installed; install from
https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter
[✓] Connected device (1 available)
Upvotes: 0
Views: 983
Reputation: 93
I know this is an old question and it already has a correct answer, but is case this helps anyone out there.
Same error happened to me because (basically) I was calling notifyListeners()
from build
method somewhere down the widget tree. Removing that call fixed the issue.
Not sure why my code worked in debug mode though.
Upvotes: 0
Reputation: 2549
I already discovered what happened, some child widget was handling a future in the build method:
@override
Widget build(BuildContext context) {
return FutureProvider<FutureBundle>.value(
value: _bloc.getChannels(),
initialData: FutureBundle(state: BundleState.LOADING),
catchError: (context, error) {
return FutureBundle(state: BundleState.ERROR, data: error);
},
child: Consumer<FutureBundle>(builder: (context, bundle, view) {
I changed this implementation following this response reference and everything worked again:
@override
Widget build(BuildContext context) {
return FutureBuilder(
future: future,
builder: (context, snapshot) {
if (snapshot.hasData) {
return ChangeNotifierProvider<WaitingBloc>.value(
value: _bloc,
child: Consumer<WaitingBloc>(builder: (context, bloc, child) {
Upvotes: 0