未附加到任何滚动视图的 ScrollController

我正在使用 CustomScrollView,并为其提供一个控制器。 ScrollController 可以工作,我甚至给它添加了一个侦听器,并打印出滚动视图的位置。

CustomScrollView(
controller: _scrollController,

现在,我要做的就是跳转到 initState ()函数中的50.0位置。

_scrollController.jumpTo(50.0);

但是,我知道错了

未附加到任何滚动视图的 scrollController

72453 次浏览

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

_scrollController = ScrollController(initialScrollOffset: 50.0);

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

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

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.

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);
}
});

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

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

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

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

Initialize the scrollController:

ScrollController _scrollController = ScrollController();

Use the code bellow where you want to scroll:

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

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

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,

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,
                          

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: [
// ....
]
)
)
)

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!

Declare an object of controller like this.

 final _scrollController = ScrollController();

And then provide it to both, to Scrollbar and to it child (ListView in this case)

Scrollbar(
thumbVisibility: true,
controller: _scrollController,
child: ListView(
controller: _scrollController,
children: [
Text("Hello World"),
Text("Hello World"),
Text("Hello World"),
],
),

Define a scroll conotroller;

final _scrollController = ScrollController();

Assign it to both controllers

Scrollbar(
controller: _scrollController,
child: ListView(
controller: _scrollController,
itemBuilder: ...
)
)