'Flutter save page state after navigator.pop

How do you save a page's state even after the back button is clicked (i.e. Navigator.pop(context))? The saved page will not call initState again when the back button is pressed and the page is revisited a second time. AutomaticKeepAliveClientMixin seems to only be able to save state between bottom navigation bar tabs.

As an example, WhatsApp wouldn't re-initialize chat screens and re-fetch messages every time one switches between different chats.



Solution 1:[1]

Simple answer you must store your state out of your page and pass state to page when routing or retrieve state using InheritedWidget , Provider vs.

check out this sample https://flutter.dev/docs/development/data-and-backend/state-mgmt/simple

Solution 2:[2]

simple way to do that is using one of state management package as provider,bloc, riverpod etc

Solution 3:[3]

Try it!

 @override
  void dispose() {
    //You can save your page here
    super.dispose();
  }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Erkan
Solution 2 abdelrahman abied
Solution 3 Rohit Soni