Skip to content

Commit

Permalink
fix(neon): fix late initialization of SelectOption.values
Browse files Browse the repository at this point in the history
  • Loading branch information
Leptopoda committed Aug 2, 2023
1 parent 91faa0a commit 52c32f4
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions packages/neon/neon/lib/src/settings/models/select_option.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,16 @@ class SelectOption<T> extends Option<T> {
required super.label,
required super.defaultValue,
required final Map<T, LabelBuilder> values,

/// Force loading the stored value.
///
/// This is needed when [values] is empty but the stored value should still be loaded.
/// This only works when [T] is of type String?.
final bool forceLoadValue = true,
super.category,
super.enabled,
}) : _values = values,
super(initialValue: _fromString(values, storage.getString(key)));
super(initialValue: loadValue(values, storage.getString(key), forceLoad: forceLoadValue));

/// Creates a SelectOption depending on the State of another [Option].
SelectOption.depend({
Expand All @@ -25,9 +31,23 @@ class SelectOption<T> extends Option<T> {
required super.defaultValue,
required final Map<T, LabelBuilder> values,
required super.enabled,

/// Force loading the stored value.
///
/// This is needed when [values] is empty but the stored value should still be loaded.
/// This only works when [T] is of type String?.
final bool forceLoadValue = true,
super.category,
}) : _values = values,
super.depend(initialValue: _fromString(values, storage.getString(key)));
super.depend(initialValue: loadValue(values, storage.getString(key), forceLoad: forceLoadValue));

static T? loadValue<T>(final Map<T, LabelBuilder> vs, final String? stored, {final bool forceLoad = true}) {
if (forceLoad && vs.isEmpty && stored is T) {
return stored as T;
}

return _fromString(vs, stored);
}

static T? _fromString<T>(final Map<T, LabelBuilder> vs, final String? valueStr) {
if (valueStr == null) {
Expand Down

0 comments on commit 52c32f4

Please sign in to comment.