openDatabase abstract method

Future<Database> openDatabase(
  1. String path, {
  2. int? version,
  3. OnVersionChangedFunction? onVersionChanged,
  4. DatabaseMode? mode,
  5. SembastCodec? codec,
})

Open a new or existing database.

path is the location of the database: a file path with databaseFactoryIo (use an absolute path in an app folder in Flutter), a database name on the web and in memory.

Opening loads the whole database in memory. Open it once and keep the Database for the lifetime of the app. Opening a path already open returns the same instance (its version and onVersionChanged are then ignored).

var db = await databaseFactoryIo.openDatabase(
  path,
  version: 1,
  onVersionChanged: (db, oldVersion, newVersion) async {
    if (oldVersion == 0) {
      // Created: add initial data here.
    }
  },
);

version is the version expected, if not null and if the existing version is different, onVersionChanged is called. When a database is created, its default version is 1.

mode is DatabaseMode.defaultMode by default.

A custom codec can be used to load/save a record, allowing for user encryption.

Implementation

Future<Database> openDatabase(
  String path, {
  int? version,
  OnVersionChangedFunction? onVersionChanged,
  DatabaseMode? mode,
  SembastCodec? codec,
});