init method
Implementation
Future<void> init() async {
const dbName = "ezto_verify.db";
final oldPath = await getApplicationDocumentsDirectory();
final oldDbFile = File(p.join(oldPath.path, dbName));
final isMigrationNeeded = await oldDbFile.exists();
final newDbPath = kIsWeb
? null
: (Platform.isIOS | Platform.isMacOS)
? await getLibraryDirectory()
: await getApplicationSupportDirectory();
if (newDbPath != null) {
if (isMigrationNeeded) {
await oldDbFile.rename(File(p.join(newDbPath.path, dbName)).path);
}
final dpPath = p.join(newDbPath.path, dbName);
try {
var codec = SembastCodec(
signature: 'encrypt',
codec: DbCodec(
password:
utf8.decode(await sl<EncryptionService>().generateDbKey()),
),
);
db = await dbFactory.openDatabase(dpPath, codec: codec);
} catch (e) {
debugPrint(e.toString());
await dbFactory.deleteDatabase(dpPath);
await init();
}
}
}