Skip to content

Commit

Permalink
Fix app preferences
Browse files Browse the repository at this point in the history
  • Loading branch information
n-bernat committed Sep 7, 2024
1 parent 033e794 commit 44d2cbc
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.3.0

- **BREAKING**: Fix type errors for a list of custom items.
- **BREAKING**: `fromJson` and `toJson` require `Object` instead of `Map<String, Object?>`.

## 0.2.0

- **BREAKING**: Require `Preference` type to extend `Object`.
Expand Down
16 changes: 4 additions & 12 deletions lib/flutter_app_preferences.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ class Preference<T extends Object> extends ValueNotifier<T> {
String key,
super.initialValue, {
List<T>? values,
T Function(Map<String, Object?> json)? fromJson,
Map<String, Object?> Function(T item)? toJson,
T Function(Object json)? fromJson,
Object Function(T item)? toJson,
}) : assert(
initialValue is! Enum || (values != null),
'You have to provide a list of possible `values` for an enum type.',
Expand All @@ -68,8 +68,8 @@ class Preference<T extends Object> extends ValueNotifier<T> {
final String _key;
final T _initialValue;
final List<T>? _values;
final Map<String, Object?> Function(T)? _toJson;
final T Function(Map<String, Object?>)? _fromJson;
final Object Function(T)? _toJson;
final T Function(Object)? _fromJson;

bool _initialized = false;
T? _value;
Expand Down Expand Up @@ -118,10 +118,6 @@ class Preference<T extends Object> extends ValueNotifier<T> {
(const (List<double>)) =>
_prefs?.getStringList(_key)?.map(double.parse).toList(),
// Custom objects
(const (List)) => _prefs
?.getStringList(_key)
?.map((v) => _fromJson!(jsonDecode(v) as Map<String, dynamic>))
.toList(),
_ => (_prefs?.containsKey(_key) ?? false)
? _fromJson!(_prefs!.getString(_key)! as Map<String, dynamic>)
: null,
Expand Down Expand Up @@ -170,10 +166,6 @@ class Preference<T extends Object> extends ValueNotifier<T> {
(newValue as List<double>).map((v) => v.toString()).toList(),
),
// Custom objects
(const (List)) => _prefs?.setStringList(
_key,
(newValue as List<T>).map((v) => jsonEncode(_toJson!(v))).toList(),
),
_ => _prefs?.setString(_key, jsonEncode(_toJson!(newValue))),
};

Expand Down

0 comments on commit 44d2cbc

Please sign in to comment.