Dispose方法未在Flutter中调用

xxe27gdn  于 2023-06-24  发布在  Flutter
关注(0)|答案(3)|浏览(336)

我面临一个问题,其中dispose方法是不调用后,在flutter。首先这里是源代码。

class Game extends StatefulWidget {

  Game({Key key, this.title}) : super(key: key);

  final String title;
  @override
  _GameState createState() => new _GameState();
}

class _GameState extends State<Game>  with SingleTickerProviderStateMixin {

  final CrosswordController myController = CrosswordController();

  var chewieController = null;
  var videoPlayerController = null;

  Widget makeVideoStreaming(){
    videoPlayerController = VideoPlayerController.network("https://somelink.com");
    chewieController = ChewieController(//paramtere here
    );
  }

  @override
  void initState() {
    super.initState();
   this.makeVideoStreaming();
    _controller =  AnimationController(vsync: this, duration: Duration(minutes: gameTime));
  }

  @override
  void dispose(){
    print('DISPOSE CALLED- GAME---');
    videoPlayerController.dispose();
    chewieController.dispose();
    _controller.dispose();
    super.dispose();
  }
  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: _onBackPressed,
      child:  Scaffold(
        key: _scaffoldKey,
        drawer: NavigationDrawer(),
        resizeToAvoidBottomPadding: false,

        body://body here
      ),
    );
  }
}

NavigationDrawer()中,i改变了一些不同的路由,就像这样。

onTap: () {
   Navigator.pop(context); Navigator.pushNamed(context, '/edit_profile');
 },

上面只是一部分代码,在点击抽屉列表项中的一个项后调用。为什么在GameState中不调用dispose方法?

jmo0nnb3

jmo0nnb31#

从堆栈中移除屏幕时调用的Dispose方法是指使用navigator.pop()pushReplacement;

wnrlj8wa

wnrlj8wa2#

这是一个已知bug:https://github.com/flutter/flutter/issues/40940
即使你从官方文档中复制示例,它也不会被调用:https://flutter.dev/docs/cookbook/networking/web-sockets#complete-example
我已经启动了flutter-dev线程,以找出是否应该使用其他东西来代替:https://groups.google.com/g/flutter-dev/c/-0QZFGO0p-g/m/bictyZWCCAAJ

s71maibg

s71maibg3#

您可以使用maintainState:MaterialPageRoute小部件中的false

相关问题