5

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 ?

Vipin Dubey
  • 379
  • 2
  • 11
  • 21

3 Answers3

17

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

Viren V Varasadiya
  • 25,492
  • 9
  • 45
  • 61
  • Then please suggest me how can i solve this issue ? I means that from navigation drawer as the screen which i sent you is the main/home screen of the app – Vipin Dubey May 05 '20 at 11:31
  • You can create a method and and call it before navigating. – Viren V Varasadiya May 05 '20 at 11:34
  • Do you want to go back to that screen when you push back button? If no then use push replacement. – Viren V Varasadiya May 05 '20 at 11:36
  • I tried that.But i arises an error ! Edit profile page now don't have back button when i used push replacement – Vipin Dubey May 05 '20 at 11:41
  • that's what i also mention. if you don't want to go back that screen then only that will work. i think only solution for you is to create a separate method and do whatever you are doing in dispose method and call it before navigating to other scree, – Viren V Varasadiya May 05 '20 at 11:47
  • 1
    incorrect: `dispose()` never gets called, even after removing app from stack as of flutter 2.2 – morgwai May 28 '21 at 21:26
2

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

morgwai
  • 2,513
  • 4
  • 25
  • 31
0

you can use maintainState: false in your MaterialPageRoute widget