Skip to content

Conversation

@xu-baolin
Copy link
Member

@xu-baolin xu-baolin commented Apr 16, 2021

Fixes #74372
Also, resolve a StackOverflow question been asked a long time ago:
https://stackoverflow.com/questions/53824944/flutter-exception-scrollcontroller-attached-to-multiple-scroll-views

Detach the position in the microtask, because there will be a delay and cause this bug.

ScrollableState._updatePosition

  void _updatePosition() {
    _configuration = widget.scrollBehavior ?? ScrollConfiguration.of(context);
    _physics = _configuration.getScrollPhysics(context);
    if (widget.physics != null) {
      _physics = widget.physics!.applyTo(_physics);
    } else if (widget.scrollBehavior != null) {
      _physics = widget.scrollBehavior!.getScrollPhysics(context).applyTo(_physics);
    }
    final ScrollPosition? oldPosition = _position;
    if (oldPosition != null) {
      _effectiveScrollController.detach(oldPosition);   

    // **********************************************
     // **********************************************
    //   During this period, the scrollcontroller is attached
    //    with two positions, which will trigger a bug.
    // **********************************************
   // **********************************************

      // It's important that we not dispose the old position until after the
      // viewport has had a chance to unregister its listeners from the old
      // position. So, schedule a microtask to do it.
      scheduleMicrotask(oldPosition.dispose);
    }

    _position = _effectiveScrollController.createScrollPosition(_physics!, this, oldPosition);
    assert(_position != null);
    _effectiveScrollController.attach(position);
  }

@flutter-dashboard flutter-dashboard bot added the framework flutter/packages/flutter repository. See also f: labels. label Apr 16, 2021
@google-cla google-cla bot added the cla: yes label Apr 16, 2021
@xu-baolin
Copy link
Member Author

@chunhtai hi, please take a look at this.: )

@override
void detach(ScrollPosition position) {
assert(position is _NestedScrollPosition);
(position as _NestedScrollPosition).detachFromParent();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The parent is set in attach method, We should reset the parent directly something like

(position as _NestedScrollPosition).setParent(null);

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you are very right.

Copy link
Contributor

@chunhtai chunhtai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@HansMuller HansMuller changed the title fix a NestedScrollView's ScrollPosotion access bug fix a NestedScrollView's ScrollPosition access bug Apr 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

framework flutter/packages/flutter repository. See also f: labels.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

<ScrollController attached to multiple scroll views> while navigating to back to a page with scrollController

3 participants