Flutter ListView 继续执行未来的函数



我的列表视图只有在向上滚动时才滞后。我的列表视图是 FurureBuilder 的子视图。我尝试了addAutomaticKeepAlives:true,因为我的数据是静态的,但它似乎没有进行任何更改。

请帮助我 当我向上滚动时,鸢尾突然跳起来。 显示它的视频: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;
}

你可以做一个类变量,假设getData的类型从你的函数getPrincipalList返回,为空,在init中用你的函数调用分配这个变量,而不等待。

Future<List<String>>? getData;
@override
void initState() {
super.initState();
getData = getPrincipalList();
}

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

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

最新更新