使用自动keepparealiveclientmixin时dispose()是调用的

发布于 2025-01-29 16:08:56 字数 1738 浏览 4 评论 0原文

我的印象是,使用自动keepparealiveclientmixin将阻止状态dispose()在窗口窗口不再可见时被调用。

但是,我有一个情况下,即使我实现了automatickeakealiveclientmixin distose() and and initstate()也会被调用。 >正确。

class IdleScreenState extends State<IdleScreen> with AutomaticKeepAliveClientMixin {

  @override
  void initState() {
    super.initState();
    print('IdleScreen initState');
  }


  @override
  void dispose() {
    print('IdleScreen dispose');
    super.dispose();
  }


  @override
  Widget build(BuildContext context) {
    super.build(context);

    // ...build the page...
  }

  @override
  bool get wantKeepAlive => true;

}

这就是我

class MainScreen extends State<MainScreen> with AutomaticKeepAliveClientMixin {
  @override
  Widget build(BuildContext context) {
    super.build(context);

    return somecondition ? IdleScreen() : OtherScreen();
  }

  @override
  bool get wantKeepAlive => true;

}

每次显示此窗口小部件(屏幕)时隐藏/显示此窗口小部件的方式,initstate()被调用,每次我隐藏它时,dispose() get get称为。好像自动keepparealiveclientmixin没有效果。我发现的所有其他类似问题似乎是由于缺少Want keepareive =&gt; truesuper.build(context),但它们在代码中为100%。

我也尝试为indlescreen提供globalkey,但这没有任何效果。

但是,如果我使用indexedStackoff stage hide/显示窗口小部件,则可以按预期工作(initstate() ()在隐藏/显示小部件时不会被调用)。

  IndexedStack(
    index: somecondition ? 0 : 1,
    children: [
      IdleScreen(),
      OtherScreen()
    ],
  ),

也许我误会了,但是自动keepakealiveclientmixin不必手动使用此技术将小部件保持在围绕窗口上的全部目的吗?

如果这很重要,这是在网络项目中。

I am under the impression that using AutomaticKeepAliveClientMixin would prevent the states dispose() callback from being called when the Widget isn't visible anymore.

However, I have a situation where dispose() and initState() get called every time I hide/show a Widget, even though I implemented AutomaticKeepAliveClientMixin correctly.

class IdleScreenState extends State<IdleScreen> with AutomaticKeepAliveClientMixin {

  @override
  void initState() {
    super.initState();
    print('IdleScreen initState');
  }


  @override
  void dispose() {
    print('IdleScreen dispose');
    super.dispose();
  }


  @override
  Widget build(BuildContext context) {
    super.build(context);

    // ...build the page...
  }

  @override
  bool get wantKeepAlive => true;

}

This is how I hide/show this Widget

class MainScreen extends State<MainScreen> with AutomaticKeepAliveClientMixin {
  @override
  Widget build(BuildContext context) {
    super.build(context);

    return somecondition ? IdleScreen() : OtherScreen();
  }

  @override
  bool get wantKeepAlive => true;

}

Every time this Widget (screen) is shown, initState()gets called, and every time I hide it, dispose() gets called. It's as if the AutomaticKeepAliveClientMixin has no effect. All other similar issues I could find seem to be due to either missing the wantKeepAlive => true or the super.build(context), but they are 100% there in the code.

I tried supplying a GlobalKey for IdleScreen as well, but that didn't have any effect.

However, if I use an IndexedStack or Offstage to hide/show the widget, it works as expected (initState() and dispose() don't get called when hiding/showing the widget).

  IndexedStack(
    index: somecondition ? 0 : 1,
    children: [
      IdleScreen(),
      OtherScreen()
    ],
  ),

Maybe I'm mistaken, but isn't the whole purpose of AutomaticKeepAliveClientMixin to not have to manually keep the widget around using this technique?

This is in a web project, if that matters.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

耳钉梦 2025-02-05 16:08:56

类型参数t是混合此类的状态的状态下类的类型

class IdleScreenState extends State<IdleScreen> 
     with AutomaticKeepAliveClientMixin <IdleScreen> {...

The type argument T is the type of the StatefulWidget subclass of the State into which this class is being mixed.

you have to pass the widget class name like this..

class IdleScreenState extends State<IdleScreen> 
     with AutomaticKeepAliveClientMixin <IdleScreen> {...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文