Database Class

Rather than having database logic mixed in randomly throughout the app, the main database handling methods are placed in lib/services/db.dart for convenience and ease of maintenance:

This class is abstract since it isn’t meant to be instantiated and only one copy of it in memory is required. Internally, it holds a reference to the SQLite database in the _db property. The database version number has been hard-coded to 1 but in more complex applications the database version can be used to migrate database schemas up or down in version to allow rollout of new features without having to wipe the database and start from scratch.

An instance of the SQLite database is created within the init method, using the example database name for this project. If the example database doesn’t exist yet, onCreate is automatically called. This is where queries for creating table structure are placed. In this case, we have a todo_items table with a primary key for id along with fields that match properties on the TodoItem class above.