@@ -162,52 +162,39 @@ abstract class PersistedState<T> implements AsyncStateNotifier<T> {
162
162
void init (ErrorHandler errorHandler, bool storageJson) {
163
163
this ._errorHandler = errorHandler;
164
164
addListener ((state) {
165
- // Only persist state if storageJson is true
166
- if (storageJson) {
167
- if (_persistance != null ) {
168
- _hasUpdated = true ;
169
- } else {
170
- _persistance = _store
171
- .setPersisted (_key, toJson (state))
172
- .whenComplete (_whenPersistenceComplete);
173
- }
165
+ if (_persistance != null ) {
166
+ _hasUpdated = true ;
167
+ } else {
168
+ print ( "storageJson = $ storageJson " ) ;
169
+ _persistance = storageJson
170
+ ? _store
171
+ .setPersisted (_key, toJson (state))
172
+ .whenComplete (_whenPersistenceComplete)
173
+ : null ;
174
174
}
175
175
});
176
176
_store.ready.then <void >((_) async {
177
- // If storage is disabled, delete any existing files to prevent data leakage
178
- if (! storageJson) {
179
- try {
180
- await _store.deletePersisted (_key);
181
- } catch (e) {
182
- // Ignore deletion errors, as the file might not exist
183
- }
184
- }
185
-
186
177
Map <String , dynamic >? rawV;
187
- // Only read from storage if storageJson is true
188
- if (storageJson) {
189
- try {
190
- rawV = await _store.getPersisted (_key);
191
- } on FormatException catch (e) {
192
- // Addressing https://github.com/segmentio/analytics_flutter/issues/74
193
- // File corruption should be less likely with removal of async code in writes
194
- // Existing corrupted files are cleaned up here without failing initialization
195
- _store.setPersisted (_key, {});
196
- log ("Clean file $_key with format error" , kind: LogFilterKind .warning);
197
- final wrappedError = ErrorLoadingStorage (e);
198
- errorHandler (wrappedError);
199
- }
178
+ try {
179
+ rawV = await _store.getPersisted (_key);
180
+ } on FormatException catch (e) {
181
+ // Addressing https://github.com/segmentio/analytics_flutter/issues/74
182
+ // File corruption should be less likely with removal of async code in writes
183
+ // Existing corrupted files are cleaned up here without failing initialization
184
+ _store.setPersisted (_key, {});
185
+ log ("Clean file $_key with format error" , kind: LogFilterKind .warning);
186
+ final wrappedError = ErrorLoadingStorage (e);
187
+ errorHandler (wrappedError);
200
188
}
201
189
T v;
202
190
203
191
if (rawV == null ) {
204
192
final init = await _initialiser ();
205
- // Only persist if storageJson is true
206
- if (storageJson) {
207
- _persistance = _store
208
- .setPersisted (_key, toJson (init))
209
- .whenComplete (_whenPersistenceComplete);
210
- }
193
+ _persistance = storageJson
194
+ ? _store
195
+ .setPersisted (_key, toJson (init))
196
+ .whenComplete (_whenPersistenceComplete)
197
+ : null ;
211
198
_notifier.nonNullState = init;
212
199
v = init;
213
200
} else {
0 commit comments