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

Unhandled Exception in _PlutoLazyPaginationState.setPage #829

Closed
s-yanev opened this issue May 3, 2023 · 3 comments
Closed

Unhandled Exception in _PlutoLazyPaginationState.setPage #829

s-yanev opened this issue May 3, 2023 · 3 comments
Labels
stale This issue is stale because it has been open for 30 days with no activity.

Comments

@s-yanev
Copy link
Contributor

s-yanev commented May 3, 2023

Describe the bug
An unhandled exception occurs when navigating to another page while PlutoLazyPagination widget is still processing an async request :

Unhandled Exception: 'package:flutter/src/widgets/scroll_controller.dart': Failed assertion: line 170 pos 12: '_positions.isNotEmpty': ScrollController not attached to any scroll views.
#0      _AssertionError._doThrowNew (dart:core-patch/errors_patch.dart:51:61)
#1      _AssertionError._throwNew (dart:core-patch/errors_patch.dart:40:5)
#2      ScrollController.jumpTo (package:flutter/src/widgets/scroll_controller.dart:170:12)
#3      _PlutoLazyPaginationState.setPage.<anonymous closure> (package:pluto_grid/src/plugin/pluto_lazy_pagination.dart:183:45)
<asynchronous suspension>

To Reproduce
Steps to reproduce the behavior:

  1. Create a PlutoGrid with a PlutoLazyPagination widget in it.
  2. Mock the fetch request to some long operation, for example:
await Future.delayed(const Duration(seconds: 5));
  1. While the async operation is working try to navigate to some other screen.

Expected behavior
Check if the ScrollController is actually attached to a view before attempting to do the jumpTo() action.

Additional context
The place where the exception happens is in the pluto_lazy_pagination.dart file, at the setPage() function, line 183 :

void setPage(int page) async {
    if (_isFetching) return;

    _isFetching = true;

    stateManager.setShowLoading(true, level: PlutoGridLoadingLevel.rows);

    widget
        .fetch(
      PlutoLazyPaginationRequest(
        page: page,
        sortColumn: stateManager.getSortedColumn,
        filterRows: stateManager.filterRows,
      ),
    )
        .then((data) {
      stateManager.scroll.bodyRowsVertical!.jumpTo(0); // <-- Unhandled Exception here

      stateManager.refRows.clearFromOriginal();
      stateManager.insertRows(0, data.rows);

      setState(() {
        _page = page;

        _totalPage = data.totalPage;

        _isFetching = false;
      });

      stateManager.setShowLoading(false);
    });
  }

It seems that when the async operation is invoked, there is a possibility that when the then() callback is called the PlutoGrid to be already removed from the screen.

One possible solution for resolving the issue can be to just check if the widget is still mounted. Something like this:

if(mounted) {
            stateManager.scroll.bodyRowsVertical!.jumpTo(0);

            stateManager.refRows.clearFromOriginal();
            stateManager.insertRows(0, data.rows);

            setState(() {
              _page = page;

              _totalPage = data.totalPage;

              _isFetching = false;
            });

            stateManager.setShowLoading(false);
          }
@s-yanev
Copy link
Contributor Author

s-yanev commented May 4, 2023

Issue is fixed with PR - #830

@github-actions
Copy link

github-actions bot commented Jun 4, 2023

This issue is stale because it has been open for 30 days with no activity.

@github-actions github-actions bot added the stale This issue is stale because it has been open for 30 days with no activity. label Jun 4, 2023
@github-actions
Copy link

This issue was closed because it has been inactive for 14 days since being marked as stale.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stale This issue is stale because it has been open for 30 days with no activity.
Projects
None yet
Development

No branches or pull requests

1 participant