导航到另一个视图时如何删除 Snackbar

How to remove Snackbar when navigating to another view

@override
  void initState() {
    super.initState();
    _showFlushbar();
  }

  _showFlushbar() async {
    await Future.delayed(Duration(milliseconds: 500));
    defaultFlushbar(
      context: context,
      message:
          'Success',
      padding: 50.0,
    );
  }

当我导航到屏幕时,它按我的预期显示了 Flushbar。但由于它有 Future.delayed 持续时间,我无法在导航到另一个视图时将其删除。如何在不更改持续时间的情况下删除它。

谢谢

扑腾中initState

Called when this object is inserted into the tree.The framework will call this method exactly once for each State object it creates.

因此您的 _showFlushbar 仅在您创建对象时被调用一次。除非您在别处引用它,否则它应该只显示一次。

由于延迟,创建小部件后显示

你可以随时处理

@override
  void dispose() {
    FlashHelper.dispose();
    super.dispose();
  }