init method

Future<void> init({
  1. required String path,
  2. bool absolutePath = false,
  3. List<String> queries = const <String>[],
  4. List<DbTable> schema = const <DbTable>[],
  5. bool verbose = false,
  6. String? fromAsset,
  7. bool debug = false,
})

Initialize the database

The database can be initialized either from an asset file with the fromAsset parameter or from a schema or from create table and other queries. Either a schema or query parameter must be provided.

Implementation

Future<void> init(
    {required String path,
    bool absolutePath = false,
    List<String> queries = const <String>[],
    List<DbTable> schema = const <DbTable>[],
    bool verbose = false,
    String? fromAsset,
    bool debug = false}) async {
  /// The [path] is where the database file will be stored. It is by
  /// default relative to the documents directory unless [absolutePath]
  /// is true.
  /// [queries] is a list of queries to run at initialization
  /// and [debug] set Sqflite debug mode on.
  ///
  /// Either a [queries] or a [schema] must be provided if the
  /// database is not initialized from an asset
  assert(path != null);
  /*if (fromAsset == null && queries.isEmpty && schema.isEmpty) {
    throw ArgumentError("Either a [queries] or a [schema] must be provided");
  }*/
  if (debug) {
    await Sqflite.setDebugModeOn(true);
  }
  var dbpath = path;
  if (!absolutePath) {
    final documentsDirectory = await getApplicationDocumentsDirectory();
    dbpath = documentsDirectory.path + "/" + path;
  }
  if (verbose) {
    print("INITIALIZING DATABASE at " + dbpath);
  }
  // copy the database from an asset if necessary
  var checkCreateQueries = false;
  if (fromAsset != null) {
    final file = File(dbpath);
    if (!file.existsSync()) {
      if (verbose) {
        print("Copying the database from asset $fromAsset");
      }
      List<int> bytes;
      try {
        // read
        final data = await rootBundle.load("$fromAsset");
        bytes =
            data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
      } catch (e) {
        throw DatabaseAssetProblem("Unable to read database from asset: $e");
      }
      try {
        // create the directories path if necessary
        if (!file.parent.existsSync()) {
          file.parent.createSync(recursive: true);
        }
        // write
        await file.writeAsBytes(bytes);
        checkCreateQueries = true;
      } catch (e) {
        throw DatabaseAssetProblem("Unable to write database from asset: $e");
      }
    }
  }
  if (this._db == null) {
    await _mutex.synchronized(() async {
      // open
      if (verbose) {
        print("OPENING database");
      }
      this._db = await openDatabase(dbpath, version: 1,
          onCreate: (Database _sqfliteDb, int version) async {
        await _initQueries(schema, queries, _sqfliteDb, verbose);
      }, onOpen: (Database _sqfliteDb) async {
        // run create queries for file copied from an asset
        if (fromAsset != null && checkCreateQueries) {
          if (schema != null || queries.isNotEmpty) {
            await _initQueries(schema, queries, _sqfliteDb, verbose);
          }
        }
      });
    });
  }
  if (verbose) {
    print("DATABASE INITIALIZED");
  }
  _dbFile = File(dbpath);
  // set internal schema
  _schema.tables = schema.toSet();
  // the database is ready to use
  if (!_readyCompleter.isCompleted) {
    _readyCompleter.complete();
  }
  _isReady = true;
}