You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When using the PagewiseLoadController in conjunction with a PagewiseListView, a setState() call is triggered during the widget's build phase. This results in the following Flutter error:
FlutterError (setState() or markNeedsBuild() called during build.
ThisPagewiseListView<UserEntity> widget cannot be marked as needing to build because the framework is already in the process of building widgets.)
This behavior originates from the initState() method in the PagewiseState class, where the setState() method is invoked during widget construction via a controller listener.
Steps to Reproduce
Add a PagewiseListView to a widget.
Pass a PagewiseLoadController or allow the widget to create its own controller.
Navigate in and out of the page containing the PagewiseListView quickly.
Observe the FlutterError during the build process caused by the setState() call in the PagewiseLoadController listener.
Expected Behavior
The widget should handle state changes without calling setState() during the widget's build phase. Updates should be deferred to ensure compliance with Flutter's state management rules.
Actual Behavior
The PagewiseLoadController listener calls setState() within the initState() method of the PagewiseState class, causing the above error.
Proposed Fix
Modify the PagewiseState implementation to defer setState() calls using WidgetsBinding.instance.addPostFrameCallback. This ensures that state updates occur after the build phase.
Updated initState() Example:
@overridevoidinitState() {
super.initState();
if (widget.pageLoadController ==null) {
this._controller =PagewiseLoadController<T>(
pageFuture: widget.pageFuture,
pageSize: widget.pageSize,
);
}
this._effectiveController!.init();
this._controllerListener = () {
if (!mounted) return; // add this// And add thisWidgetsBinding.instance.addPostFrameCallback((_) {
if (mounted) {
setState(() {});
}
});
};
this._effectiveController!.addListener(this._controllerListener);
}
When using the PagewiseLoadController in conjunction with a PagewiseListView, a setState() call is triggered during the widget's build phase. This results in the following Flutter error:
This behavior originates from the initState() method in the PagewiseState class, where the setState() method is invoked during widget construction via a controller listener.
Steps to Reproduce
Expected Behavior
The widget should handle state changes without calling setState() during the widget's build phase. Updates should be deferred to ensure compliance with Flutter's state management rules.
Actual Behavior
The PagewiseLoadController listener calls setState() within the initState() method of the PagewiseState class, causing the above error.
Proposed Fix
Modify the PagewiseState implementation to defer setState() calls using WidgetsBinding.instance.addPostFrameCallback. This ensures that state updates occur after the build phase.
Updated initState() Example:
Environment
Flutter version: 3.24.5 • channel stable
flutter_pagewise version: ^2.0.4
Dart version: Dart SDK version: 3.5.4 (stable)
PR
#120
The text was updated successfully, but these errors were encountered: