7

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 ?

3
  • Try to just push to the edit_profile page and check if it works? Commented May 5, 2020 at 11:00
  • No it didn't worked Commented May 5, 2020 at 11:20
  • 1
    your dispose is not called because somehow that widget is kept in the widget tree, try to use Navigator.pushReplacement('the replacement'), Commented May 5, 2020 at 11:35

3 Answers 3

17

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

Sign up to request clarification or add additional context in comments.

6 Comments

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
You can create a method and and call it before navigating.
Do you want to go back to that screen when you push back button? If no then use push replacement.
I tried that.But i arises an error ! Edit profile page now don't have back button when i used push replacement
incorrect: dispose() never gets called, even after removing app from stack as of flutter 2.2
|
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

Comments

0

you can use maintainState: false in your MaterialPageRoute widget

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.