ScrollController not attached to any scroll views

Flutter

Flutter Problem Overview


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

Flutter Solutions


Solution 1 - Flutter

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 - Flutter

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 - Flutter

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

_scrollController = ScrollController(initialScrollOffset: 50.0);

Solution 4 - Flutter

@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 - Flutter

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,
      ),
    ),
  ),

Solution 6 - Flutter

This sometimes happens when you are attempting to bind a ScrollController to a widget that doesn't actually exist (yet). So it attempts to bind, but there is no ListView/ScrollView to bind to. Say you have this code:

Column(
  crossAxisAlignment: CrossAxisAlignment.start,
  children: <Widget>[
    Expanded(
      flex: 8,
      child: Container(
        child: ListView.builder(
          controller: scrollController,
          itemCount: messages.length,
          itemBuilder: (context, index) {
            return MessageTile(message: messages[index]);
          }),
        ),
     ),
 ]),

Here we are building a ListView dynamically. Since we never declared this ListView before, the scrollController does not have anything to bind to. But this can easily be fixed:

// first declare the ListView and ScrollController

final scrollController = ScrollController(initialScrollOffset: 0);
ListView list = ListView.builder(
  controller: scrollController,
  itemCount: messages.length,
  itemBuilder: (context, index) {
    return MessageTile(message: messages[index]);
  }
);

// then inside your build function just reference the already built list

Column(
  crossAxisAlignment: CrossAxisAlignment.start,
  children: <Widget>[
    Expanded(
      flex: 8,
      child: Container(
         child: list,
      ),
    ),
  ]),

Solution 7 - Flutter

I have tried with above solutions but set ScrollController initialScrollOffset, checking hasClients and jumpTo, WidgetsBinding no one is working for me.

at last solved my problem by checking positions 'scrollcontroller.positions.length' like

if (_sc.positions.length == 0 || _sc.position.pixels == 0.0) {
	return Container();
}

You have to check controller positions to get rid of the error

Reference : https://api.flutter.dev/flutter/widgets/ScrollController/position.html

Solution 8 - Flutter

Initialize the scrollController:

ScrollController _scrollController = ScrollController(); 

Use the code bellow where you want to scroll:

SchedulerBinding.instance.addPostFrameCallback((_) {
  _scrollController.jumpTo(_scrollController.position.maxScrollExtent);
});

Solution 9 - Flutter

I found working solution, but frankly the method is not best practice, I suppose. In my case controller.jumpTo() was called before it was attached to any scroll view. In order to solve problem I delayed some milliseconds and then call .jumpTo(), because build will be called and controller will be attached to any scroll view.

Future.delayed(Duration(milliseconds: <some time, ex: 100>), () {
         _scrollController.jumpTo(50.0);
        });

I full agree that it is bad solution, but It can solve problem.

Solution 10 - Flutter

Use setState() method to bind to a scroll related widget (your listview etc.)

setState(() {
    topicsScrollController.animateTo(....);
});

Solution 11 - Flutter

I used this code to make the color of the appbar transparent at 0 position and black for more than 70

backgroundColor: !_scrollController.hasClients
            ? Colors.transparent
            : _scrollController.offset > 70
                ? Color.fromRGBO(7, 7, 7, 1)
                : Colors.transparent,

Solution 12 - Flutter

I had this issue when using a ListView with a ScrollBar. The solution was to explicitly set the same controller to both the ScrollBar and ListView

final ScrollController _controller = ScrollController();

CupertinoScrollbar(
  controller: _controller,
  child: ListView.builder(
    controller: _controller,
                          

Solution 13 - Flutter

I solving using the same controller in Scrollbar() e ListView() widgets.

Container(
    height: 140,
    child: Scrollbar(
        controller: listviewScrollbarController,
        child: ListView(
            scrollDirection: Axis.horizontal,
            controller: listviewScrollbarController,
            children: [
                // ....
            ]
        )
    )
)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionOmar AwamryView Question on Stackoverflow
Solution 1 - FlutterEd HView Answer on Stackoverflow
Solution 2 - Fluttercarlosx2View Answer on Stackoverflow
Solution 3 - FlutterboformerView Answer on Stackoverflow
Solution 4 - FlutterAbdullah KhanView Answer on Stackoverflow
Solution 5 - FlutterLucas De Lavra PintoView Answer on Stackoverflow
Solution 6 - FlutteraverroesView Answer on Stackoverflow
Solution 7 - FlutterMimu Saha TishanView Answer on Stackoverflow
Solution 8 - FlutterShariar SaimonView Answer on Stackoverflow
Solution 9 - FlutterYerkebulan DuisebayView Answer on Stackoverflow
Solution 10 - FlutterSaya GamersView Answer on Stackoverflow
Solution 11 - FlutterMohamed Aziz MarzoukiView Answer on Stackoverflow
Solution 12 - FlutterMobileMonView Answer on Stackoverflow
Solution 13 - FlutterSilas Ribas MartinsView Answer on Stackoverflow