Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dispose method not called in flutter

I am facing an issue in which dispose method is not called after changing screen in flutter .First of all here is the source code.

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
      ),
    );
  }
}

In NavigationDrawer() i changes to some different route something like this.

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

Above is just a part of code which is called after clicking on one of the item from drawer list item. In GameState dispose method is not called why ?

like image 897
Vipin Dubey Avatar asked Dec 10 '22 00:12

Vipin Dubey


2 Answers

Dispose method called when you remove screen from stack mean's that when you use navigator.pop() Or pushReplacement;

like image 92
Viren V Varasadiya Avatar answered Jan 31 '23 18:01

Viren V Varasadiya


it's a known bug: https://github.com/flutter/flutter/issues/40940
even if you copy examples from official docs, it will not get called: https://flutter.dev/docs/cookbook/networking/web-sockets#complete-example
I've started a thread of flutter-dev to find out if something else should be used instead: https://groups.google.com/g/flutter-dev/c/-0QZFGO0p-g/m/bictyZWCCAAJ

like image 31
morgwai Avatar answered Jan 31 '23 19:01

morgwai