我的屏幕上有一个listView.我已经附加了一个控制器.我可以调用我的端点,接收响应,解析它并插入行. ListView应该自动滚动.确实如此,但并非完美无缺.我总是背后的一个项目.这是我的代码:
@override Widget build(BuildContext context) { // Scroll to the most recent item if (equationList.length > 0) { _toEnd(); } return new Scaffold( appBar: new AppBar( title: new Text(widget.title),),body: EquList(equationList,_scrollController),floatingActionButton: new FloatingActionButton( onPressed: onFabClick,tooltip: 'Fetch Post',child: new Icon(isLoading ? Icons.pause : Icons.play_arrow),); } void _toEnd() { _scrollController.animateTo( _scrollController.position.maxScrollExtent,duration: const Duration(milliseconds: 250),curve: Curves.ease,); }
问题是,我在最后一项插入列表之前调用_toEnd()函数.所以,我正在寻找一个回调(如果有的话)告诉我build()完成了.然后我调用我的_toEnd()函数.
在这种情况下,最佳做法是什么?
解决方法
详细说明
Günter’s评论:
@override Widget build(BuildContext context) { executeAfterBuild(); return Container(); } Future<void> executeAfterBuild() async { // this code will get executed after the build method // because of the way async functions are scheduled }
有一个很好的例子illustrating that effect here.
关于Dart can be found here中的日程安排的广泛信息.