加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

在Flutter中完成“构建”功能时是否有任何回调告诉我?

发布时间:2020-12-14 14:48:18 所属栏目:百科 来源:网络整理
导读:我的屏幕上有一个listView.我已经附加了一个控制器.我可以调用我的端点,接收响应,解析它并插入行. ListView应该自动滚动.确实如此,但并非完美无缺.我总是背后的一个项目.这是我的代码: @override Widget build(BuildContext context) { // Scroll to the mo
我的屏幕上有一个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中的日程安排的广泛信息.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读