From ea8f6ba62ae7b48994d6ce98d6ffe9081ad37adb Mon Sep 17 00:00:00 2001 From: SliverAppBar Date: Mon, 6 Nov 2023 23:42:13 +0900 Subject: [PATCH] db: fix database manager for proper initial database downloading --- lib/database/database.dart | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/lib/database/database.dart b/lib/database/database.dart index dd4373388..efd1edf6e 100644 --- a/lib/database/database.dart +++ b/lib/database/database.dart @@ -20,9 +20,10 @@ class DataBaseManager { @protected @mustCallSuper - void dispose() async { + Future dispose() async { print('close: ${dbPath!}'); - if (db != null) db!.close(); + await db?.close(); + db = null; } static Future getInstance() async { @@ -37,18 +38,19 @@ class DataBaseManager { } static Future reloadInstance() async { - var dbPath = Platform.isAndroid - ? '${(await getApplicationDocumentsDirectory()).path}/data/data.db' - : '${await getDatabasesPath()}/data.db'; - _instance = create(dbPath); + final db = _instance?.db; + _instance?.db = null; + await db?.close(); } - Future open() async { + Future open() async { db ??= await openDatabase(dbPath!); } - Future checkOpen() async { - if (!db!.isOpen) db = await openDatabase(dbPath!); + Future checkOpen() async { + if (!(db?.isOpen ?? false)) { + db = await openDatabase(dbPath!); + } } Future>> query(String str) async {