【问题标题】:Stateful widget dispose method not called未调用有状态小部件处置方法
【发布时间】:2021-01-17 00:37:26
【问题描述】:

如果我从屏幕上滑出小部件,我有一个列表。在滑动时,我从列表中删除了第一个小部件,但我意识到该小部件的 dispose 方法没有被调用,因此 AnimationContoller 仍然由颤动持有并用于后续小部件。后续小部件的动画在它们被带到前面时结束。这使我的应用程序无法按预期运行。为了测试这一点,我在小部件的 initState 和 dispose 方法中打印了一个字符串。 initState 打印但 dispose 方法不打印。有什么帮助吗?

class ProfileWidget extends StatefulWidget {

  ProfileWidget({Key key}) : super(key: key);

  @override
  State<StatefulWidget> createState() => _ProfileWidgetState();
}

class _ProfileWidgetState extends State<ProfileWidget> with TickerProviderStateMixin {
    
  @override
  void initState() {
    super.initState();
    print("New init");

    _animationController = new AnimationController(
      vsync: this,
      duration: const Duration(milliseconds: 400),
    );

    _fadeAnimationController = new AnimationController(
      vsync: this,
      duration: const Duration(milliseconds: 200),
    );

    _fadeAnimation = new Tween(
      begin: 0.0,
      end: 1.0,
    ).animate(_fadeAnimationController);

    _fadeAnimationController.forward();

  }


  @override
  void dispose() {
    print("Disposing");

    if (_animationController != null) {
      _animationController.dispose();
    }

    if (_fadeAnimationController != null) {
      _fadeAnimationController.dispose();
    }

    super.dispose();
  }
}


【问题讨论】:

    标签: flutter


    【解决方案1】:

    这是 Flutter 中的一个已知错误,正在讨论中:https://github.com/flutter/flutter/issues/40940
    即使您从官方文档中复制示例,它也不会被调用:https://flutter.dev/docs/cookbook/networking/web-sockets#complete-example
    然而,flutter 开发人员声称这是一种理想的行为。如果您认为应该修复,请在 github 问题中表达您的支持。

    【讨论】:

    • 谢谢。我一定会这样做的。
    猜你喜欢
    • 2021-12-24
    • 2018-12-04
    • 2020-10-19
    • 2021-01-22
    • 1970-01-01
    • 1970-01-01
    • 2021-02-16
    • 2021-06-24
    • 1970-01-01
    相关资源
    最近更新 更多