Avoid setState during build by deferring options update in didUpdateWidget #2147
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
optionsindidUpdateWidgetto the next frame to avoidsetState()during build re-entrancy inMapInteractiveViewer.Problem
FlutterMap.didUpdateWidgetsetscontroller.optionsduring the parent’s build. This triggers the controller’s listeners, which callMapInteractiveViewerState.setState()whileFlutterMapis still building.MapInteractiveViewerFlutterMap._setMapController / didUpdateWidget → MapControllerImpl.options= → MapInteractiveViewerState.onMapStateChange → setState()Root cause
MapControllerImpl.optionsupdates internal state and notifies listeners immediately. When invoked insidedidUpdateWidget(i.e., during the parent’s build), the notification re-enters the widget tree and triggerssetState()in a descendant before the parent completes its build.Change
didUpdateWidgetto post-frame:lib/src/map/widget.dartWhy this fix works
MapInteractiveViewer, eliminating the build-cycle re-entrancy.Backwards compatibility
Verification
didUpdateWidget.MapInteractiveViewerupdates on the next frame as expected.Related work
flutter_map_animationsdefers the firstAnimationController.forward()to post-frame to avoid first-animation races accessing theMapInteractiveViewerbefore it’s fully attached. This PR stands alone to fix the build-cycle assertion; animations fix is independent. Defer AnimationController.forward() to post-frame in AnimatedMapController.animateTo TesteurManiak/flutter_map_animations#51