Creating a Data Source

To create a Data Source for storing our notes in a local SQL database, we need to create the Data Source itself and a few Contract classes. The Contract classes will define the structure, helper methods, and constants for our SQLite database and its tables.

1. Creating the Contracts

In the note_app/modules/infrastructure/lib/data_sources/sqlbrite/contracts/ directory, create the following contract files:

1.1. Create a contract for our note database

Create a contract class named notes_database_contract.dart to define the name and version of our database:

/// {@template NotesDatabaseContract}
/// A contract class for the notes SQL database.
/// {@endtemplate}
class NotesDatabaseContract {
  /// {@macro NotesDatabaseContract}
  NotesDatabaseContract._();

  /// The name of the database.
  static const databaseName = 'notes.db';

  /// The current version of the database.
  static const databaseVersion = 1;
}

1.2. Create a contract for our note table

Create a contract class named note_entry_contract.dart for our notes table to define its structure and available query commands:

2. Create the Data Source

Finally, create the Data Source to allow for CRUD (create, read, update, delete) operations on our local database.

Under the note_app/modules/infrastructure/lib/data_sources/sqlbrite directory, create a file called sqlbrite_data_source.dart and copy the following code:

Last updated