4

I'm building a social media app. A user named Fred might

  1. download Fred's own profile and see it on the screen
  2. look at Fred's own followers
  3. see that Sarah is following Fred
  4. tap on Sarah's name to download Sarah's profile
  5. look at Sarah's followers
  6. see that Fred is following Sarah
  7. 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!!!

1 Answer 1

2

I think that what you are looking for is State Management. Once you're familiar with flutter basics, you have to work with it.

I would start with Provider.

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

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.