Creating a Data Model
Let's create a Data Model for our note entry that conforms to the structure of our Data Source.
The BriteDatabase
from the sqlbrite package allows us to create and fetch data from the SQL database in JSON format. Hence, we need to add a JSON serialization/deserialization feature to our NoteEntryModel
.
In addition, we will also include a feature for mapping the Data Model
into an Entity and vice-versa.
Under the note_app/modules/infrastructure/lib/data_sources/sqlbrite/
directory, create a file named note_entry_model.dart
and paste the following code:
Notice that the NoteEntryModel.updateTimestamp
and NoteEntry.updatedAt
serve the same purpose but have different names and data types?
This is a good example for highlighting the difference between an Entity
and Data Model
. That is, an Entity
is entirely independent from a Data Source, whereas a Data Model
is defined from a specific Data Source
.
Once done, build the JSON serializable class for the NoteEntryModel
using the build_runner
:
Tip: You can use a script to run the build_runner
for multiple modules in one go.
Last updated