changeDB method

Future<void> changeDB(
  1. String? newDBName
)

Switches the application database to newDBName.

  • If newDBName already exists it is opened and becomes the current application database until the next call to changeDB.
  • Otherwise it is created, opened and becomes the current database.

The name (with the .db suffix appended if missing) is persisted to disk so that subsequent calls to db return the same database.

Implementation

Future<void> changeDB(String? newDBName) async {
  if (newDBName == null || newDBName.isEmpty) {
    debugPrint('changeDB called with a null or empty name.');
    return;
  }

  final String correctedName = newDBName.endsWith('.db')
      ? newDBName
      : '$newDBName.db';

  await _backend.writePointer(correctedName);

  await closeDB();
  final Future<OpenedDatabase> opening = _openApplicationDatabase(
    name: correctedName,
  );
  _opening = opening;
  await opening;
  debugPrint('Database successfully changed to $correctedName');
}