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.
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:
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
@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
});
}
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
I had this problem, but in my case was much simpler, althought I think it worth mention:
Don't forget to attach the controller to your widget. In my case I rearrange my screens and change the PageView. No widgets bindings would fix it because it was not point to any!