is State.dispose() deprecated?

628 views
Skip to first unread message

Piotr Morgwai Kotarbinski

unread,
May 28, 2021, 5:21:12 PM5/28/21
to Flutter Development (flutter-dev)
Hi all,
Many examples in the official docs use `State.dispose()` to free resources, like for example this websocket example closes the connection: https://flutter.dev/docs/cookbook/networking/web-sockets#complete-example

However, if you copy/paste this example into your project and add `debugPrint('disposing')` somewhere in the method, you will notice that it does not get called when you navigate away from the app on an android device or close app's window on desktop. There's an open issue about it, but no one is assigned and there has been no real activity since 2019: https://github.com/flutter/flutter/issues/40940

Since releasing some resources may be a critical issue, I guess it means, that `State.dispose()` was deprecated and some other, reliable way should be used, but I couldn't find any authoritative info about it: could someone provide a link to an official documentation that explains how to reliably release resources on app exit?

Thanks!

Piotr Morgwai Kotarbinski

unread,
May 31, 2021, 11:42:54 AM5/31/21
to Flutter Development (flutter-dev)
in a similar issue a claim was made that this is a desired behavior:

The dispose method on State should not be called when the app quits.

The behaviour of the following three cases should all be the same:

  • Hitting the home button
  • Hitting the back button on the first page
  • Switching applications in the app switcher
However, as I pointed in comments to both issues, the behavior in the first case will never be the same in general because of how android works:
in the 2nd and 3rd case, the app will generally not be terminated immediately (unless the system is tight on resources). Therefore if a user navigates back to the app via 'recent apps' switcher, the old connection will still be open (unless server closes it due to application specific timeout of client inactivity) and the server will not even notice that a user navigated away and returned again.
However, in the first case, the app is always terminated immediately and all its OS resources released. It may be removed from switcher or not (depending on android version/flavor, but not sure here). If it's not removed and a user navigates back to it, the old state will be already discarded by this time, a new instance of state will be created and a new connection to the server will be established.
Therefore, since the state is always destroyed in the 1st case (but not necessarily in 2nd and 3rd case), I claim that there should be a reliable way to release state's child resources according to general rule of object ownership mentioned before. Since `dispose` method already exists and many devs expect it to be exactly this mechanism, I claim that `dispose` method should be guaranteed to be called on app exit.
If there is a desire to unify behavior in the above 3 cases, then the only way to achieve this is by reliably terminating app in all 3 cases and thus reliably calling `dispose` also. (which personally I think is a terrible idea: I don't want to lose app's state whenever I switch to another for 10 seconds to copy some text).

I welcome others to provide their opinion/insight.

Thanks!
Reply all
Reply to author
Forward
0 new messages