我如何 update/refresh 在执行像点击一行这样的操作后查看 ListView 的视图

How can I update/refresh the view of a ListView after performing an action like a tap on a row

我正在关注 Flutter 网站上的 saved/favourite 教程

代码:

void _pushFavourites() {
    Navigator.of(context).push(
      MaterialPageRoute<void>(builder: (BuildContext ctx) {
        var tiles = _favourites.map((WordPair pair) {
          return ListTile(
            title: Text(pair.asCamelCase, style: _fontStyle),
            onTap: () {
              // ⬇︎ probably here I should update tiles, but I don't know how to do it
              setState(() {
                _favourites.remove(pair);
              });
            },
          );
        });
        final divided = ListTile.divideTiles(
          context: ctx,
          tiles: tiles,
        ).toList();

        return Scaffold(
          appBar: AppBar(title: Text('Favourite Suggestions')),
          body: ListView(children: divided,)
        );
        
      })
    );
  }

整个代码是here

您正在调用父路由的 setState(具有功能 _pushFavourites 的小部件)

为这部分代码创建一个单独的有状态小部件,它应该可以工作:

var tiles = _favourites.map((WordPair pair) {
          return ListTile(
            title: Text(pair.asCamelCase, style: _fontStyle),
            onTap: () {
              // ⬇︎ probably here I should update tiles, but I don't know how to do it
              setState(() {
                _favourites.remove(pair);
              });
            },
          );
        });
        final divided = ListTile.divideTiles(
          context: ctx,
          tiles: tiles,
        ).toList();

        return Scaffold(
          appBar: AppBar(title: Text('Favourite Suggestions')),
          body: ListView(children: divided,)
        );