Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A bug that causes corrupted prefs-file due to simultaneous writes is … #5

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions lib/src/preferences_io.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'dart:async';
import 'dart:convert';
import 'dart:io';

Expand Down Expand Up @@ -92,6 +93,17 @@ class IOPreferences extends Preferences {
preferenceCache[key] = value;
}

_markToSave();
return true;
}

Timer lastMarked;
_markToSave() {
lastMarked?.cancel();
lastMarked = Timer(Duration(milliseconds: 20), _makeSave);
}

_makeSave() async {
if (filePath != null) {
final file = File(filePath);
if (isCrypted) {
Expand All @@ -100,10 +112,9 @@ class IOPreferences extends Preferences {
try {
await file.writeAsString(json.encode(preferenceCache));
} catch (err) {
return false;
print(err);
}
}
return true;
}

/// Completes with true once the user preferences for the app has been cleared.
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: crypted_preferences
description: Flutter preferences management with crypto capabilities
version: 0.0.2
version: 0.0.3
homepage: https://github.com/jaumard/crypted_preferences
repository: https://github.com/jaumard/crypted_preferences
author: jaumard <[email protected]>
Expand Down