initDB method

Future<Database?> initDB()

This method is used to initialize the database. It first gets the application documents directory and then constructs the path of the database file. It then opens the database and creates the tables if they do not exist. It also handles the database upgrade process.

Implementation

Future<Database?> initDB() async {
  Directory documentDirectory = await getApplicationDocumentsDirectory();

  final path = join(documentDirectory.path, '${DbParameters.dbName}.db');

  return await openDatabase(
    path,
    version: DbParameters.dbVersion,
    onOpen: (db) {},
    onCreate: (db, version) async {
      // For each table in DbParameters.tables, it creates the table if it does not exist.
      await createDatabase(db);
    },
    onUpgrade: (db, oldVersion, newVersion) async {
      switch (oldVersion) {
        default:
          // For each table in DbParameters.tables, it creates the table if it does not exist.
          await createDatabase(db);
      }
    },
  );
}