I'm building a social media app. A user named Fred might
- download Fred's own profile and see it on the screen
- look at Fred's own followers
- see that Sarah is following Fred
- tap on Sarah's name to download Sarah's profile
- look at Sarah's followers
- see that Fred is following Sarah
- tap on Fred's name to see his own profile again.
At this point, the stack of screens in the app is the following
Fred < Sarah < Fred
I want the User instance in both Fred screens to be the very same instance. Since a User is a ChangeNotifier if Fred makes a change to his own account on the top Fred profile screen, and presses the back button twice, then the bottom Fried profile screen should show the changes made.
The stack of user screens could become arbitrarily deep as the user navigates through the app.
My idea is that when a UserScreen widget asks for the User object, the program first checks in an in-memory cache of all User objects currently in use before calling the central server's JSON API.
I want User objects to be garbage collected when no longer needed by any Widget.
How can I keep an in-memory cache that doesn't require manually incrementing and decrementing reference counts in widget initState and dispose methods? Is there something like a weak set in Dart that I can use?
Thanks!!!