Drift queries result objects - enhancing the PODO (plain old dart object) #2964
Replies: 1 comment 1 reply
-
The JSON trick is not something I'm super happy with - it sounds like something that could break quietly (and IMO, adding serialization to generated row classes was a mistake - it's convenient, but terrible for separation of concerns).
You can enable the But since drift can map both tables and result sets into existing row classes, I wonder if you can use the view models you already have and let drift map rows into those instead? If you're already writing model classes with the features you need, letting drift use them as well feels like the cleanest solution. |
Beta Was this translation helpful? Give feedback.
-
I use BLoC, and I need my view state to refresh when the underlying database entity has changed.
I achieve this by mapping the drift entity to a view model class using a simple toJson/fromJson operation - this is possible because drift Table and View results implement json serialization.
My view model class then uses Equatable to ensure all fields are considered during equality comparisons.
It's nice and simple.
i.e. var model = ModelType.fromJson(entity.toJson())
I have started to use .drift queries to shape the data more before mapping it to a viewmodel - however I have hit a roadblock - while drift tables implement
toJson
to extract a simple field dict - query result classes generated by drift queries do not.So I guess I have a couple of questions:
Thoughts?
Beta Was this translation helpful? Give feedback.
All reactions