Skip to content

Commit 35fc9b1

Browse files
committed
Change to assert if not part of the valid set.
1 parent 8499899 commit 35fc9b1

File tree

2 files changed

+133
-49
lines changed

2 files changed

+133
-49
lines changed

packages/flutter/lib/src/widgets/restoration_properties.dart

Lines changed: 74 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,9 @@ class RestorableTextEditingController extends RestorableChangeNotifier<TextEditi
505505
/// The values are serialized using the name of the enum, obtained using the
506506
/// [EnumName.name] extension accessor.
507507
///
508-
/// The represented value is accessible via the [value] getter.
508+
/// The represented value is accessible via the [value] getter. The set of
509+
/// values in the enum are accessible via the [values] getter. Since
510+
/// [RestorableEnumN] allows null, this set will include null.
509511
///
510512
/// See also:
511513
///
@@ -515,20 +517,45 @@ class RestorableEnumN<T extends Enum> extends RestorableValue<T?> {
515517
/// Creates a [RestorableEnumN].
516518
///
517519
/// {@macro flutter.widgets.RestorableNum.constructor}
518-
RestorableEnumN(T? defaultValue, { required this.allowedValues })
519-
: _defaultValue = defaultValue;
520+
RestorableEnumN(T? defaultValue, { required Iterable<T> values })
521+
: _defaultValue = defaultValue,
522+
values = values.toSet();
520523

521524
@override
522525
T? createDefaultValue() => _defaultValue;
523526
final T? _defaultValue;
524527

525-
/// The set of allowed values that this [RestorableEnumN] may represent.
528+
@override
529+
set value(T? newValue) {
530+
assert(newValue == null || values.contains(newValue),
531+
'Attempted to set an unknown enum value "$newValue" that is not null, or '
532+
'in the valid set of enum values for the $T type: '
533+
'${values.map<String>((T value) => value.name).toSet()}');
534+
super.value = newValue;
535+
}
536+
537+
/// The set of non-null values that this [RestorableEnumN] may represent.
538+
///
539+
/// This is a required field that supplies the enum values that are serialized
540+
/// and restored.
541+
///
542+
/// If a value is encountered that is not null or a value in this set,
543+
/// [fromPrimitives] will assert when restoring.
544+
///
545+
/// It is typically set to the `values` list of the enum type.
546+
///
547+
/// In addition to this set, because [RestorableEnumN] allows nullable values,
548+
/// null is also a valid value, even though it doesn't appear in this set.
526549
///
527-
/// {@macro flutter.widgets.RestorableEnum.allowedValues}
550+
/// {@tool snippet} For example, to create a [RestorableEnumN] with an
551+
/// [AxisDirection] enum value, with a default value of null, you would build
552+
/// it like the code below:
528553
///
529-
/// In addition to this list, because [RestorableEnumN] allows nullable
530-
/// values, the set of allowed values will also include null.
531-
Iterable<T> allowedValues;
554+
/// ```dart
555+
/// RestorableEnumN<AxisDirection> axis = RestorableEnumN<AxisDirection>(null, values: AxisDirection.values);
556+
/// ```
557+
/// {@end-tool}
558+
Set<T> values;
532559

533560
@override
534561
void didUpdateValue(T? oldValue) {
@@ -541,11 +568,15 @@ class RestorableEnumN<T extends Enum> extends RestorableValue<T?> {
541568
return null;
542569
}
543570
if (data is String) {
544-
for (final T allowed in allowedValues) {
571+
for (final T allowed in values) {
545572
if (allowed.name == data) {
546573
return allowed;
547574
}
548575
}
576+
assert(false,
577+
'Attempted to set an unknown enum value "$data" that is not null, or '
578+
'in the valid set of enum values for the $T type: '
579+
'${values.map<String>((T value) => value.name).toSet()}');
549580
}
550581
return _defaultValue;
551582
}
@@ -573,26 +604,43 @@ class RestorableEnum<T extends Enum> extends RestorableValue<T> {
573604
/// Creates a [RestorableEnum].
574605
///
575606
/// {@macro flutter.widgets.RestorableNum.constructor}
576-
RestorableEnum(T defaultValue, { required this.allowedValues })
577-
: _defaultValue = defaultValue;
607+
RestorableEnum(T defaultValue, { required Iterable<T> values })
608+
: _defaultValue = defaultValue,
609+
values = values.toSet();
578610

579611
@override
580612
T createDefaultValue() => _defaultValue;
581613
final T _defaultValue;
582614

583-
/// The set of allowed values that this [RestorableEnum] may represent.
615+
@override
616+
set value(T newValue) {
617+
assert(values.contains(newValue),
618+
'Attempted to set an unknown enum value "$newValue" that is not in the '
619+
'valid set of enum values for the $T type: '
620+
'${values.map<String>((T value) => value.name).toSet()}');
621+
622+
super.value = newValue;
623+
}
624+
625+
/// The set of values that this [RestorableEnum] may represent.
584626
///
585-
/// {@template flutter.widgets.RestorableEnum.allowedValues}
586-
/// This is a required field that determines which enum values may be
587-
/// serialized and restored.
627+
/// This is a required field that supplies the possible enum values that can
628+
/// be serialized and restored.
588629
///
589-
/// If a value is encountered that is not in this set, the default value
590-
/// supplied to the constructor is substituted for it.
630+
/// If a value is encountered that is not in this set, [fromPrimitives] will
631+
/// assert when restoring.
591632
///
592-
/// It is typically set to the `values` list of the enum type, but may also be
593-
/// a subset of those values.
594-
/// {@endtemplate}
595-
Iterable<T> allowedValues;
633+
/// It is typically set to the `values` list of the enum type.
634+
///
635+
/// {@tool snippet} For example, to create a [RestorableEnum] with an
636+
/// [AxisDirection] enum value, with a default value of [AxisDirection.up],
637+
/// you would build it like the code below:
638+
///
639+
/// ```dart
640+
/// RestorableEnum<AxisDirection> axis = RestorableEnum<AxisDirection>(AxisDirection.up, values: AxisDirection.values);
641+
/// ```
642+
/// {@end-tool}
643+
Set<T> values;
596644

597645
@override
598646
void didUpdateValue(T? oldValue) {
@@ -602,11 +650,15 @@ class RestorableEnum<T extends Enum> extends RestorableValue<T> {
602650
@override
603651
T fromPrimitives(Object? data) {
604652
if (data != null && data is String) {
605-
for (final T allowed in allowedValues) {
653+
for (final T allowed in values) {
606654
if (allowed.name == data) {
607655
return allowed;
608656
}
609657
}
658+
assert(false,
659+
'Attempted to restore an unknown enum value "$data" that is not in the '
660+
'valid set of enum values for the $T type: '
661+
'${values.map<String>((T value) => value.name).toSet()}');
610662
}
611663
return _defaultValue;
612664
}

0 commit comments

Comments
 (0)