3

I have a NestedScrollView that houses a SliverAppBar with a TabBarView and the tabs consist of an infinite loading list each. Right now I only have one ScrollController attached to NestedScrollView, and list widgets read the scroll position of this controller.

The infinite loading logic uses controller.position.extentAfter to decide when to fetch data from the API. But with multiple tabs, I get the error

ScrollController attached to multiple scroll views.

I tried reading about controller.positions but couldn't make sense of the 2 line documentation that's available. My question is, is it possible to access scroll positions per page in a TabBarView or should I just use separate ScrollController for each of those and forget about the correct scrolling of slivers?

3
  • Moved to a new layout without the slivers. Leaving the question, if anyone has an answer to this. Commented Dec 17, 2018 at 20:20
  • you're going to have to provide a sample for us to see, but there are other similar questions with the same error: stackoverflow.com/questions/52484710/…, stackoverflow.com/questions/53824944/…, stackoverflow.com/questions/52170779/… Commented Feb 6, 2020 at 19:00
  • @TWL This is more than a year old man. I don't even remember what approach I ended up taking. And unsure what was my reasons for asking this either. Commented Feb 11, 2020 at 15:30

1 Answer 1

8

You can't use the same controller for different scroll views. This is the reason you are getting this error "ScrollController attached to multiple scroll views." However, you can listen to the PrimaryScrollController and update your infinite list to fetch more data from the API. If you look at the code of NestedScrollView you would find the PrimaryScrollController there.

NestedScrollView(
  body: Builder(builder: (BuildContext context) {
    final innerScrollController = PrimaryScrollController.of(context);
    // Use the innerScrollController to listen to the scrolling.
    // This would be your controller for list. You can listen to this controller to know whether the list has reached maxScrollExtent and fetch data from API.
    }),
 );

For reference:

The [body] is built in a context that provides a [PrimaryScrollController] that interacts with the [NestedScrollView]'s scroll controller. Any [ListView] or other [Scrollable]-based widget inside the [body] that is intended to scroll with the [NestedScrollView] should therefore not be given an explicit [ScrollController], instead allowing it to default to the [PrimaryScrollController] provided by the [NestedScrollView].

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.