From 52c32f422a132f3d3abc6107de0b2910aebae145 Mon Sep 17 00:00:00 2001 From: Nikolas Rimikis Date: Wed, 19 Jul 2023 12:24:58 +0200 Subject: [PATCH] fix(neon): fix late initialization of SelectOption.values --- .../src/settings/models/select_option.dart | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/packages/neon/neon/lib/src/settings/models/select_option.dart b/packages/neon/neon/lib/src/settings/models/select_option.dart index 5d3fc7da..e1d7e110 100644 --- a/packages/neon/neon/lib/src/settings/models/select_option.dart +++ b/packages/neon/neon/lib/src/settings/models/select_option.dart @@ -12,10 +12,16 @@ class SelectOption extends Option { required super.label, required super.defaultValue, required final Map 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({ @@ -25,9 +31,23 @@ class SelectOption extends Option { required super.defaultValue, required final Map 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(final Map 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(final Map vs, final String? valueStr) { if (valueStr == null) {