Vipin Dubey
Vipin Dubey

Reputation: 409

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 ?

Upvotes: 7

Views: 13846

Answers (3)

Mostafa Abdelazim
Mostafa Abdelazim

Reputation: 1

you can use maintainState: false in your MaterialPageRoute widget

Upvotes: 0

morgwai
morgwai

Reputation: 2803

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

Upvotes: 2

Viren V Varasadiya
Viren V Varasadiya

Reputation: 27137

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

Upvotes: 17

Related Questions