Skip to content

Commit cb3ada4

Browse files
WahdanZphillwiggins
authored andcommitted
fix corestore db on desktop (#203)
fix core corestore path if db password changed , the db will remove and return new instance
1 parent 7c97afb commit cb3ada4

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

lib/src/storage/core_store_sem_impl.dart

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,27 @@ class CoreStoreSembastImp implements CoreStore {
44
CoreStoreSembastImp._internal(this._store);
55
static CoreStoreSembastImp _instance;
66

7-
static Future<CoreStore> getInstance({DatabaseFactory factory, String password = 'flutter_sdk'}) async {
8-
7+
static Future<CoreStore> getInstance(
8+
{DatabaseFactory factory, String password = 'flutter_sdk'}) async {
99
if (_instance == null) {
1010
factory ??= databaseFactoryIo;
1111
final SembastCodec codec = getXXTeaSembastCodec(password: password);
12-
String dbDirectory = '';
13-
if (Platform.isIOS || Platform.isAndroid)
14-
dbDirectory = (await getApplicationDocumentsDirectory()).path;
15-
final String dbPath = path.join('$dbDirectory/parse', 'parse.db');
16-
final Database db = await factory.openDatabase(dbPath, codec: codec);
12+
String dbPath = '';
13+
Database db;
14+
if (Platform.isIOS || Platform.isAndroid) {
15+
final String dbDirectory =
16+
(await getApplicationDocumentsDirectory()).path;
17+
dbPath = path.join('$dbDirectory/parse', 'parse.db');
18+
} else {
19+
dbPath = path.join('parse', 'parse.db');
20+
}
21+
try {
22+
db = await factory.openDatabase(dbPath, codec: codec);
23+
} catch (e) {
24+
print(e);
25+
await factory.deleteDatabase(dbPath);
26+
db = await factory.openDatabase(dbPath, codec: codec);
27+
}
1728
_instance = CoreStoreSembastImp._internal(db);
1829
}
1930

@@ -96,4 +107,4 @@ class CoreStoreSembastImp implements CoreStore {
96107
Future<void> setStringList(String key, List<String> values) {
97108
return _store.put(values, key);
98109
}
99-
}
110+
}

0 commit comments

Comments
 (0)