我有 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;
    });
  }

最佳答案

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

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”。

关于flutter - 按下后退按钮时如何清除数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56505512/

10-11 19:22
查看更多