按下后退按钮时如何清除数据

Mic*_*ael 5 dart flutter

我有 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;
    });
  }
Run Code Online (Sandbox Code Playgroud)

小智 6

返回页面(路由)时触发操作的方式有两种。

1)

void WaitforBeingBackfromConfig() async{
    await Navigator.push(
      context, MaterialPageRoute(
        builder: (context) => ConfigScreen('platzhalter'))
    );

//Do something right after return to page1
  }
Run Code Online (Sandbox Code Playgroud)

2)正如@Michael之前所说:WillPopScope:

Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: _SaveAndBack,
      child: Scaffold(
        appBar: AppBar(
          title: Text('Config'),
        ),
        body: Column(children: <Widget>[
     ....    ]),
      ),
    );
Run Code Online (Sandbox Code Playgroud)

这会在 page2 离开之前调用函数“_SaveAndBack”。