Flutter 滚动未来的列表视图

Flutter 滚动未来的列表视图,flutter,dart,Flutter,Dart,我正在创建一个包含项目列表的页面。我想在列表呈现后滚动到页面底部。问题是我正在使用FutureBuilder获取列表。如果我使用WidgetsBinding.addPostFrameCallback这似乎是在build()方法完成之前被调用的(可能是因为它仍在获取数据的过程中) 是否有一种方法可以确定未来已经完成,列表已经呈现,然后才触发向下滚动 @override void initState() { WidgetsBinding.instance.addPostFrameCallbac

我正在创建一个包含项目列表的页面。我想在列表呈现后滚动到页面底部。问题是我正在使用FutureBuilder获取列表。如果我使用
WidgetsBinding.addPostFrameCallback
这似乎是在
build()
方法完成之前被调用的(可能是因为它仍在获取数据的过程中)

是否有一种方法可以确定未来已经完成,列表已经呈现,然后才触发向下滚动

@override
void initState() {

  WidgetsBinding.instance.addPostFrameCallback((_) => _postInit());

  super.initState();
}

Future<void> _postInit() async {
 _scrollController.jumpTo(_scrollController.position.maxScrollExtent);
}

@override
Widget build(BuildContext context) {
  return Scaffold(
    body: FutureBuilder(
      future: _getList(),
      builder: (BuildContext context, AsyncSnapshot snapshot) {
        switch(snapshot.connectionState) {
          case ConnectionState.none:
          case ConnectionState.waiting:
            return Center(child: SizedBox(height: 100, width: 100, child: new CircularProgressIndicator()));
            break;
          default:
            if(snapshot.hasError) {
              return Container();
            } else {
              return _buildListView(snapshot.data);
            }
          }
      }

    )
  );
}

Widget _buildListView(list) {
  return Column(
    children: <Widget>[
      Expanded(
        child: ListView(
          controller: _scrollController,
          shrinkWrap: true,
          children: list,
        ),
      ),
    ],
  );
}
@覆盖
void initState(){
WidgetsBinding.instance.addPostFrameCallback((\u)=>\ u postInit());
super.initState();
}
Future\u postInit()异步{
_scrollController.jumpTo(_scrollController.position.maxScrollExtent);
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
正文:未来建设者(
未来:_getList(),
生成器:(BuildContext上下文,异步快照){
交换机(快照.连接状态){
案例连接状态。无:
案例连接状态。正在等待:
返回中心(子项:SizedBox(高度:100,宽度:100,子项:new CircularProgressIndicator());
打破
违约:
if(snapshot.hasError){
返回容器();
}否则{
返回_buildListView(snapshot.data);
}
}
}
)
);
}
小部件构建列表视图(列表){
返回列(
儿童:[
扩大(
子:ListView(
控制器:\ u滚动控制器,
收缩膜:对,
儿童:名单,
),
),
],
);
}

尝试删除
WidgetsBinding.instance.addPostFrameCallback((\u)=>\ u postInit())
initState
方法

之后,在
\u postInit()
方法中添加一个
delayed
函数,并在收到数据时调用该函数以显示列表,如下所示:

Future<void> _postInit() async {
   await Future.delayed(Duration(milliseconds: 100));
 _scrollController.jumpTo(_scrollController.position.maxScrollExtent);
}


@override
Widget build(BuildContext context) {
  return Scaffold(
    body: FutureBuilder(
      future: _getList(),
      builder: (BuildContext context, AsyncSnapshot snapshot) {
        switch(snapshot.connectionState) {
          case ConnectionState.none:
          case ConnectionState.waiting:
            return Center(child: SizedBox(height: 100, width: 100, child: new CircularProgressIndicator()));
            break;
          default:
            if(snapshot.hasError) {
              return Container();
            } else {
              _postInit();
              return _buildListView(snapshot.data);
            }
          }
      }

    )
  );
}
Future\u postInit()异步{
等待未来。延迟(持续时间(毫秒:100));
_scrollController.jumpTo(_scrollController.position.maxScrollExtent);
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
正文:未来建设者(
未来:_getList(),
生成器:(BuildContext上下文,异步快照){
交换机(快照.连接状态){
案例连接状态。无:
案例连接状态。正在等待:
返回中心(子项:SizedBox(高度:100,宽度:100,子项:new CircularProgressIndicator());
打破
违约:
if(snapshot.hasError){
返回容器();
}否则{
_postInit();
返回_buildListView(snapshot.data);
}
}
}
)
);
}

当然可以,添加一个简单的代码来重现问题,这样我们可以帮助您编写代码