ScrollController not attached to any scroll views
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,
),
),
),
![Omar Awamry](https://i.stack.imgur.com/hcg3H.jpg?s=256&g=1)
Comments
-
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 over 5 yearsPost the exact code you are using in a new question and tag me
-
sh0umik almost 5 yearsbetter solution is suggested below. First check if it is attached or not by
if(_scrollController.hasClients)
then you should calljumpTo
oranimateTo
. On android it still works if you skip client checking .. but on IOS it will cause problem. -
Rony Tesler about 3 yearsNot working for me. Scrollbar( isAlwaysShown: true, controller: controller, child: ListView( controller: controller,
-
ShadeToD about 3 yearsIn Dart 2.12 instance can be null. "The method 'addPostFrameCallback' can't be unconditionally invoked because the receiver can be 'null'. "
-
Vladimir Demirev almost 3 yearsI 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 almost 3 yearsMy mistake was that I attached the controller to
Scrollbar
instead ofSingleChildScrollView
child: Scrollbar( child: SingleChildScrollView( controller: _scrollController,
. I thought I should share in case any of you might have a similar situation. -
Golden Lion over 2 yearshow do you get the listview.builder to always show the scrollbar
-
Sajas Mohammed over 2 yearsthe 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 over 2 yearsUse the unconditional call operator like so:
WidgetsBinding.instance?.addPostFrameCallback
-
mamena tech about 2 yearsbest solution for this case, thanks a lot
-
Polymath about 2 yearsThis only works it it is inside the WidgetsBinding.instance.addPostFrameCallback() as answeredd by @carlosx2