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 called during build when using PagewiseLoadController #119

Open
DomingoMG opened this issue Dec 21, 2024 · 0 comments
Open

setState called during build when using PagewiseLoadController #119

DomingoMG opened this issue Dec 21, 2024 · 0 comments

Comments

@DomingoMG
Copy link

DomingoMG commented Dec 21, 2024

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. 
This PagewiseListView<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

  1. Add a PagewiseListView to a widget.
  2. Pass a PagewiseLoadController or allow the widget to create its own controller.
  3. Navigate in and out of the page containing the PagewiseListView quickly.
  4. 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:

@override
void initState() {
  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 this
    WidgetsBinding.instance.addPostFrameCallback((_) {
      if (mounted) {
        setState(() {});
      }
    });
  };

  this._effectiveController!.addListener(this._controllerListener);
}

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

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

1 participant