ScrollController not attached to any scroll views

44,451

Solution 1

Check if the scrollController is attached to a scroll view by using its hasClients property first.

if (_scrollController.hasClients) 
    _scrollController.jumpTo(50.0);

Solution 2

Delaying it is not the right solution. Better to wait till the tree is done building by using

WidgetsBinding.instance
        .addPostFrameCallback((_){});

sample

WidgetsBinding.instance.addPostFrameCallback((_) {
      if(pageController.hasClients){
              pageController.animateToPage(page index, duration: Duration(milliseconds: 1), curve: Curves.easeInOut);
      }
});

Solution 3

To set the initial position of a ScrollController, use the initialScrollOffset property:

_scrollController = ScrollController(initialScrollOffset: 50.0);

Solution 4

@carlosx2 answer is correct but if someone wonder where to put WigetsBinding. So here it is.

@override
void initState(){
  super.initState();
  WidgetsBinding.instance.addPostFrameCallback((_){
    //write or call your logic
    //code will run when widget rendering complete
  });
}

Solution 5

I resolved my problem using the same ScrollController to both controllers. First I create the ScrollController():

ScrollController scollBarController = ScrollController();

Inside my code:

Container(
    width: MediaQuery.of(context).size.width,
    height: MediaQuery.of(context).size.height,
    child: Scrollbar(
      controller: scollBarController,
      isAlwaysShown: true,
      child: StaggeredGridView.count(
        controller: scollBarController,
        crossAxisCount: 16,
        staggeredTiles: _staggeredTamanho,
        shrinkWrap: true,
        children: [
          ...
        ], //listStaggered,
        mainAxisSpacing: 7.0,
      ),
    ),
  ),
Share:
44,451
Omar Awamry
Author by

Omar Awamry

loading CV...

Updated on July 05, 2022

Comments

  • Omar Awamry
    Omar Awamry almost 2 years

    I'm using CustomScrollView, and providing it with a controller. ScrollController works, I even added a listener to it and print out the position of the scroll view.

    CustomScrollView(
        controller: _scrollController,
    

    Now, all i'm trying to do is jump to position 50.0 inside initState() function.

    _scrollController.jumpTo(50.0);
    

    But, i get the error

    scrollController not attached to any scroll views

  • boformer
    boformer over 5 years
    Post the exact code you are using in a new question and tag me
  • sh0umik
    sh0umik almost 5 years
    better solution is suggested below. First check if it is attached or not by if(_scrollController.hasClients) then you should call jumpTo or animateTo. On android it still works if you skip client checking .. but on IOS it will cause problem.
  • Rony Tesler
    Rony Tesler about 3 years
    Not working for me. Scrollbar( isAlwaysShown: true, controller: controller, child: ListView( controller: controller,
  • ShadeToD
    ShadeToD about 3 years
    In Dart 2.12 instance can be null. "The method 'addPostFrameCallback' can't be unconditionally invoked because the receiver can be 'null'. "
  • Vladimir Demirev
    Vladimir Demirev almost 3 years
    I dont see it causing problems in IOS (at least in 14) and Flutter 2.5. Unless you add a Scrollbar() as a parent - for me, that caused the exception in IOS.
  • Adrian Moisa
    Adrian Moisa almost 3 years
    My mistake was that I attached the controller to Scrollbar instead of SingleChildScrollView child: Scrollbar( child: SingleChildScrollView( controller: _scrollController, . I thought I should share in case any of you might have a similar situation.
  • Golden Lion
    Golden Lion over 2 years
    how do you get the listview.builder to always show the scrollbar
  • Sajas Mohammed
    Sajas Mohammed over 2 years
    the page didn't move the index page after add the Callback function. i have removed the call back function and add the if(pageController.hasClients){ pageController.animateToPage(page index, duration: Duration(milliseconds: 1), curve: Curves.easeInOut); } its working now.
  • Akora Ing. DKB
    Akora Ing. DKB over 2 years
    Use the unconditional call operator like so: WidgetsBinding.instance?.addPostFrameCallback
  • mamena tech
    mamena tech about 2 years
    best solution for this case, thanks a lot
  • Polymath
    Polymath about 2 years
    This only works it it is inside the WidgetsBinding.instance.addPostFrameCallback() as answeredd by @carlosx2