Flutter ListView继续执行future函数

bjg7j2ky  于 2023-06-30  发布在  Flutter
关注(0)|答案(1)|浏览(153)

只有当我向上滚动时,我的ListView才会滞后。我的列表视图是FurureBuilder的子视图。我尝试添加AutomaticKeepAlives:因为我的数据是静态的,但它似乎没有改变。
请帮助我当我向上滚动时,scoll突然跳了起来。一段视频显示:https://streamable.com/1cjg3

class NavigatePageState extends State<NavigatePage> with AutomaticKeepAliveClientMixin {
  @override
  Widget build(BuildContext context) {
    return CupertinoPageScaffold(
      child: Center(
        child: FutureBuilder(
          future: getPrincipalList(),
          builder: (context, snapshot) {
            if (snapshot.data == null) {
              return Column(
                children: <Widget>[
                  Center(
                    child: Text('Loading...') ,
                  )
                  ,
                  Container(
                    margin: EdgeInsets.only(top:100),
                    child: PlatformCircularProgressIndicator(
                      android: (_) => MaterialProgressIndicatorData(),
                      ios: (_) => CupertinoProgressIndicatorData(),
                    ),
                  )
                ],
              );
            } else {
              return ListView.builder(
                addAutomaticKeepAlives: true,
                addRepaintBoundaries: false,
                shrinkWrap: true,
                physics: BouncingScrollPhysics(),
                itemCount: snapshot.data.result.length,

                itemBuilder: (context, rowInt) {
                  return Column(
                    children: <Widget>[
                      Container(
                          child: sliderOfSongs(
                        topName: snapshot.data.result[rowInt].infoDetail.name,
                        topDescription:
                            snapshot.data.result[rowInt].infoDetail.description,
                        collectionName: snapshot
                            .data.result[rowInt].infoDetail.collectionName,
                        topIndex: rowInt,
                      )),
                      Divider(
                        color: Colors.blueGrey,
                        indent: 120,
                        endIndent: 120,
                      )
                    ],
                  );
                },
              );
            }
          },
        ),
      ),
      navigationBar: CupertinoNavigationBar(
        middle: Text('Navigate'),
      ),
    );
  }
  @override
  bool get wantKeepAlive => true;
}
42fyovps

42fyovps1#

你可以做一件事,使一个类变量,假设getData的类型,从你的函数getPrincipalList返回与nullable,在初始化分配这个变量与你的函数调用没有等待。

Future<List<String>>? getData;

@override
void initState() {
   super.initState();
   getData = getPrincipalList();
}

并在函数代码中将此变量设置为null。并在FutureBuilder的future属性中传递此变量,就像将null传递给FutureBuilder的future一样,它会重新构建自己。

Future<List<String>> getPrincipalList(){
  ....
  getData = null;
  return [];
}

相关问题