initializeDatabase method

Future<Database> initializeDatabase()

Implementation

Future<Database> initializeDatabase() async {
  var databasesPath = await getDatabasesPath();
  var path = join(databasesPath, "demo_asset_example.db");

// Check if the database exists
  var exists = await databaseExists(path);

  if (!exists) {
    // Should happen only the first time you launch your application
    print("Creating new copy from asset");

    // Make sure the parent directory exists
    try {
      await Directory(dirname(path)).create(recursive: true);
    } catch (_) {}

    // Copy from asset
    ByteData data = await rootBundle
        .load(join("packages/bible_sqlite/assets/", "ACF.sqlite"));
    List<int> bytes =
        data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);

    // Write and flush the bytes written
    await File(path).writeAsBytes(bytes, flush: true);
  } else {
    print("Opening existing database");
  }
// open the database
  return await openDatabase(path, readOnly: true);
}