我有2页,而这些页面中的每一个都具有initState
返回列表的相同功能。
第一页具有常规功能,第二页具有相同功能,但具有返回已过滤列表的过滤器。
问题是,当我按下第二页上的“后退按钮”并返回第一页时,我得到的是过滤列表而不是常规列表。
在转到第一页之前,我如何从第二页“清除”数据?
功能:
Future fetchProducts({flag = false}) {
return http
.get(
'MyAddress')
.then<Null>((http.Response response) {
final Map<String, dynamic> listData = json.decode(response.body);
listData.forEach((String id, dynamic fetchedData) {
final Product product = Product(
id: id,
address: fetchedData['address'],
image: fetchedData['imageUrl'],
);
fetchedData.add(product);
});
_product = flag
? fetchedData.where((Product product) {
return product.userId == 1;
}).toList()
: fetchedData;
});
}
返回页面(路由)时有两种触发动作的方式。
1)
void WaitforBeingBackfromConfig() async{
await Navigator.push(
context, MaterialPageRoute(
builder: (context) => ConfigScreen('platzhalter'))
);
//Do something right after return to page1
}
2)如@Michael所述:WillPopScope:
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: _SaveAndBack,
child: Scaffold(
appBar: AppBar(
title: Text('Config'),
),
body: Column(children: <Widget>[
.... ]),
),
);
这将在离开page2之前调用函数“ _SaveAndBack”。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句