@@ -3,21 +3,24 @@ part of flutter_data;
33class LocalStorage {
44 LocalStorage ({
55 required this .baseDirFn,
6- LocalStorageClearStrategy ? clear,
6+ this . clear = LocalStorageClearStrategy .whenError ,
77 this .busyTimeout = 5000 ,
8- }) : clear = clear ?? LocalStorageClearStrategy .never ;
8+ });
99
1010 var isInitialized = false ;
1111
1212 final FutureOr <String > Function () baseDirFn;
1313 final LocalStorageClearStrategy clear;
1414 final int busyTimeout;
1515
16- late final String path;
17- late final bool inIsolate;
16+ String path = '' ;
17+ bool inIsolate = false ;
18+ bool triedOnceAfterError = false ;
19+
20+ Database ? _db;
1821
1922 @protected
20- late final Database db ;
23+ Database get db => _db ! ;
2124
2225 Future <LocalStorage > initialize ({bool inIsolate = false }) async {
2326 if (isInitialized) return this ;
@@ -31,7 +34,7 @@ class LocalStorage {
3134 await destroy ();
3235 }
3336
34- db = sqlite3.open (path, mutex: false );
37+ _db = sqlite3.open (path, mutex: false );
3538
3639 if (inIsolate) {
3740 db.execute ('''
@@ -75,11 +78,13 @@ class LocalStorage {
7578 }
7679 } catch (e, stackTrace) {
7780 print ('[flutter_data] Failed to open:\n $e \n $stackTrace ' );
78- if (clear == LocalStorageClearStrategy .whenError) {
81+ if (triedOnceAfterError == false &&
82+ clear == LocalStorageClearStrategy .whenError) {
7983 dispose ();
8084 await destroy ();
8185 await initialize ();
8286 }
87+ triedOnceAfterError = true ;
8388 }
8489
8590 isInitialized = true ;
@@ -97,7 +102,8 @@ class LocalStorage {
97102 }
98103
99104 void dispose () {
100- db.dispose ();
105+ _db? .dispose ();
106+ isInitialized = false ;
101107 }
102108}
103109
0 commit comments