getLocalDatabase static method

Future<LocalDatabase> getLocalDatabase()

Implementation

static Future<LocalDatabase> getLocalDatabase() async {
  // sql from there
  String? databasesRootPath;
  if (defaultTargetPlatform == TargetPlatform.iOS) {
    // as suggested in https://pub.dev/documentation/sqflite/latest/sqflite/getDatabasesPath.html
    final Directory directory = await getLibraryDirectory();
    databasesRootPath = directory.path;
  } else if (Platform.isLinux || Platform.isWindows) {
    sqfliteFfiInit();
    databaseFactory = databaseFactoryFfi;
  }

  databasesRootPath ??= await getDatabasesPath();

  final String databasePath = join(databasesRootPath, 'smoothie.db');
  final Database database = await openDatabase(
    databasePath,
    version: 9,
    singleInstance: true,
    onUpgrade: _onUpgrade,
  );

  final LocalDatabase localDatabase = LocalDatabase._(database);

  // only hive from there
  await Hive.initFlutter();
  final List<AbstractDao> daos = <AbstractDao>[
    DaoHiveProduct(localDatabase),
    DaoProductList(localDatabase),
    DaoStringList(localDatabase),
    DaoString(localDatabase),
    DaoInstantString(localDatabase),
    DaoInt(localDatabase),
    DaoStringListMap(localDatabase),
    DaoTransientOperation(localDatabase),
    DaoTransientFolksonomy(localDatabase),
  ];
  for (final AbstractDao dao in daos) {
    dao.registerAdapter();
  }
  for (final AbstractDao dao in daos) {
    await dao.init();
  }

  // Migrations here
  // (no migration for the moment)

  return localDatabase;
}