Flutter/Dart In-memory Object Caching?

1,513

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.

Share:
1,513
Ted Henry
Author by

Ted Henry

Struggling along building mobile apps.

Updated on December 17, 2022

Comments

  • Ted Henry
    Ted Henry over 1 year

    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!!!