Search code examples
flutterdartmethodscallbackdispose

Is the back button the same as dispose() method?


flutter

Is the back button the same as dispose() method ? I know there's not much similarity, but I mean, when you hit the back button, does that free up memory from the tree of the page we left?


Solution

  • Considering that you're talking about the Framework's BackButton widget, then eventually, why?

    Because the framework's back button is just an abstraction of creating your own widget with an IconButton that calls Navigator.maybePop(context); when it is pressed.

    To answer your question: yes, if the page is closed when BackButton is pressed, then yes it will release resources (it will call dispose() if your widget is a stateful widget).

    Keep in mind that your still need to override the dispose() method and release any resource (if any) so that those resources are release when the back button is pressed.

    To sum up::: BackButton simply calls Navigator.maybePop(context); and it will only release resources of your StatefulWidget IF you overrode it AND call some_class.dispose().