Search code examples
flutterdartstreamsembast

How do I listen to a single value inside a map within Sembast record?


The documentation talks about listening to a record, but how do I listen to a single field inside that record? Assuming the record contains map.

Supposed I have a User class as follows:

class User {

final String uid; // used as key in Sembast
final String name;
bool isVerified;

/// Used to store data to Sembast
Map<String, dynamic> toMap() => {
   'uid': uid,
   'name': name,
   'isVerified': isVerified,
};


}

When I save it locally like this:

final _store = stringMapStoreFactory.store('userStore');

// Store data:
await _store.record(user.uid).put(database, user.toMap());

Then somewhere in the page I want to listen to changes to isVerified field without fetching the whole User information. How do I do that?

Because _store.record(user.uid).onSnapshot(database) returns Stream<RecordSnapshot> of the whole data of that User class.

Thanks


Solution

  • Similarly to firestore, you can only listen to a whole record change (or a query in a store) but not a single field change in a record. As a side note, fetching a "whole" record does not cost much, the record being already ready to use in memory.