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

setState() or markNeedsBuild() called during build #607

Closed
Pwuts opened this issue Aug 18, 2022 · 11 comments
Closed

setState() or markNeedsBuild() called during build #607

Pwuts opened this issue Aug 18, 2022 · 11 comments

Comments

@Pwuts
Copy link
Contributor

Pwuts commented Aug 18, 2022

The following assertion was thrown while dispatching notifications for HeaderNotifier:
setState() or markNeedsBuild() called during build.

This ValueListenableBuilder<IndicatorState?> 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: ValueListenableBuilder<IndicatorState?>
    state: _ValueListenableBuilderState<IndicatorState?>#395da
The widget which was currently being built when the offending call was made was: _BodyBuilder
When the exception was thrown, this was the stack
#0      Element.markNeedsBuild.<anonymous closure>      package:flutter/…/widgets/framework.dart:4476
#1      Element.markNeedsBuild                          package:flutter/…/widgets/framework.dart:4491
#2      State.setState                                  package:flutter/…/widgets/framework.dart:1129
#3      _ValueListenableBuilderState._valueChanged      package:flutter/…/widgets/value_listenable_builder.dart:182
#4      IndicatorStateListenable._onNotify              package:easy_refresh/…/indicator/indicator.dart:232
#5      ChangeNotifier.notifyListeners                  package:flutter/…/foundation/change_notifier.dart:324
#6      IndicatorNotifier._update                       package:easy_refresh/…/notifier/indicator_notifier.dart:375
#7      _EasyRefreshState.didUpdateWidget               package:easy_refresh/src/easy_refresh.dart:310
#8      StatefulElement.update                          package:flutter/…/widgets/framework.dart:4992

When the widget tree is rebuilt for whatever reason, and the _EasyRefreshState is associated to a new widget, didUpdateWidget is called which ends up triggering _ValueListenableBuilderState._valueChanged, scheduling another rebuild, causing this error.

I'm not sure, but this might be solved by being more selective in calling notifyListeners() on indicator_notifier.dart:375. E.g. call it when the state changes, but not when _headerNotifier._update() is called to attach a new widget.

Possibly related: #579

I can ignore this error and the app will work fine, but during debugging this constantly triggers my debug tools.

@xuelongqy
Copy link
Owner

Can you provide a complete reproducible code example?

@zebraoo
Copy link

zebraoo commented Feb 24, 2023

Future.delayed(Duration.zero, () async {
myFunction();
});

This can be solved

@fikretsengul
Copy link

I have the same issue. Did you able to find a solution?

@xuelongqy
Copy link
Owner

I have the same issue. Did you able to find a solution?

Can you provide a reproduction case?

@fikretsengul
Copy link

fikretsengul commented Jan 19, 2024

@xuelongqy You can checkout my fork of super_cupertino_navigation_bar library where I implemented pull to refresh to mimic iOS refresher. (In the nested scroll example of the app, you can pull to refresh and check the debug console when you hot reload the app with any change)

https://github.com/fikretsengul/super_cupertino_navigation_bar/tree/dev

I used the listener pattern of easy_refresh library. But when the widget tree is rebuilt for whatever reason (like changing a text which leads a hot reload), and the _EasyRefreshState is associated to a new widget, didUpdateWidget is called which ends up triggering _ValueListenableBuilderState._valueChanged, scheduling another rebuild, causing this error.

Screenshot 2024-01-19 at 13 24 09

Like @Pwuts said, we can ignore this error and the app will work fine, but during debugging this constantly throws exception to the debug console.

@fikretsengul
Copy link

@xuelongqy Sorry to bother you again, but when will there be an update on this?

@xuelongqy
Copy link
Owner

@xuelongqy Sorry to bother you again, but when will there be an update on this?

Can you give me a reproducible case?

@fikretsengul
Copy link

fikretsengul commented Jan 25, 2024

You can checkout latest test version of my boilerplate on test branch to check this error out:

https://github.com/fikretsengul/flutter_advanced_boilerplate/tree/test

Commands to run in order to run app:
(You should use fvm, or remove "sdkPath: .fvm/flutter_sdk" line from melos.yaml)

fvm use 3.16.5
fvm flutter pub get
melos bs (This will run fvm flutter run on every package and also build_runner)

Then run the app from app/main.dart. Don't forget to do a hot reload to see the error.

@xuelongqy
Copy link
Owner

You can checkout latest test version of my boilerplate on test branch to check this error out:

https://github.com/fikretsengul/flutter_advanced_boilerplate/tree/test

Commands to run in order to run app: (You should use fvm, or remove "sdkPath: .fvm/flutter_sdk" line from melos.yaml)

fvm use 3.16.5 fvm flutter pub get melos bs (This will run fvm flutter run on every package and also build_runner)

Then run the app from app/main.dart. Don't forget to do a hot reload to see the error.

I don't have a macOS device at the moment and I'm just doing a tentative fix. You can use the latest code from the v3 branch for testing.

@fikretsengul
Copy link

fikretsengul commented Jan 26, 2024

@xuelongqy Thank you, I can confirm that it's working smoothly now. Thank you very much for your fast response, interest and for this wonderful library.

@xuelongqy
Copy link
Owner

Fixed in 3.3.5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants