Dart In-memory Object Caching (WeakMap?)

400 views
Skip to first unread message

Ted Henry

unread,
Feb 12, 2020, 4:56:15 AM2/12/20
to Dart Misc
Hi,

I asked on StackOverflow but that didn't work out. Hopefully posting here will be more fruitful.

I'm building a social media app using Flutter. While this might look like a Flutter question, I think I really want to know if the Dart language itself has some WeakMap or weak reference features that I can put to use.

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.

8. 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 `WeakMap` in Dart that I can use?

Thanks!!!

Ted

Razvan Cristian Lung

unread,
Feb 12, 2020, 7:58:57 AM2/12/20
to mi...@dartlang.org
Read about redux or provider. This is definitely a Flutter question. More specifically a state management question. 

--
For more ways to connect visit https://www.dartlang.org/community
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+uns...@dartlang.org.
To view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/misc/3208fd09-4a4d-40ef-9c0e-57d0501d71a3%40dartlang.org.

Guyren Howe

unread,
Feb 12, 2020, 11:14:22 AM2/12/20
to 'Lasse R.H. Nielsen' via Dart Misc
On Feb 12, 2020, at 2:18 , Razvan Cristian Lung <lung....@gmail.com> wrote:

I'm building a social media app using Flutter. While this might look like a Flutter question, I think I really want to know if the Dart language itself has some WeakMap or weak reference features that I can put to use.

It doesn’t.

This is because Javascript doesn’t. But it’s coming to Javascript at which point it will probably come to Dart.

William Hesse

unread,
Feb 12, 2020, 11:53:31 AM2/12/20
to Dart Misc, Guyren Howe
The Expando class https://api.flutter.dev/flutter/dart-core/Expando-class.html
is a weak map that maps objects to values, where a key will be dropped if there are no other references to it and it is garbage collected.

You cannot use numbers, strings, or booleans as keys, and you would want to pick a key that would disappear when your screen disappears, but can be used to look up the object.  I'm not sure how you would design this, or how a weak map would work.

If your screens each contain a reference to a User object, you seem to have exactly the cache of active User objects you are looking for: iterate over all active screens, to see if one has a User object for the user you want.  I don't see a way to index them by name (a String) so they can be looked up directly, since Expandos can't use Strings as keys.

Ted Henry

unread,
Feb 15, 2020, 2:59:01 PM2/15/20
to Dart Misc, guy...@gmail.com
> If your screens each contain a reference to a User object, you seem to have exactly the cache of active User objects you are looking for:

It is true, however...

> iterate over all active screens, to see if one has a User object for the user you want.

That's a lot of looking around through a bunch of screens potentially of different types with different members with different naming holding different model objects. I was hoping to find a way to do this in a centralized cache location (a.k.a. in-memory database) that would be easier to manage but without garbage accumulating. I was hoping Dart itself would provide some assistance.

("iterate over all active screens". Hmm. I don't actually know how to do that.)

Thanks.
Reply all
Reply to author
Forward
0 new messages