flutter

ScrollController not attached to any scroll views


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


Solution

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

    if (_scrollController.hasClients) 
        _scrollController.jumpTo(50.0);
    
    
    
      // small change using "Recursion"
      void _scrollDown() {
        if (controllerListDay.hasClients) {
          controllerListDay.jumpTo(controllerListDay.position.maxScrollExtent);
          return;
        }
    
        Future.delayed(Duration(seconds: 1)).then((val) => _scrollDown());
      }
    

    // just call _scrollDown() at any time