@@ -505,7 +505,9 @@ class RestorableTextEditingController extends RestorableChangeNotifier<TextEditi
505
505
/// The values are serialized using the name of the enum, obtained using the
506
506
/// [EnumName.name] extension accessor.
507
507
///
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.
509
511
///
510
512
/// See also:
511
513
///
@@ -515,20 +517,45 @@ class RestorableEnumN<T extends Enum> extends RestorableValue<T?> {
515
517
/// Creates a [RestorableEnumN] .
516
518
///
517
519
/// {@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 ();
520
523
521
524
@override
522
525
T ? createDefaultValue () => _defaultValue;
523
526
final T ? _defaultValue;
524
527
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.
526
549
///
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:
528
553
///
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;
532
559
533
560
@override
534
561
void didUpdateValue (T ? oldValue) {
@@ -541,11 +568,15 @@ class RestorableEnumN<T extends Enum> extends RestorableValue<T?> {
541
568
return null ;
542
569
}
543
570
if (data is String ) {
544
- for (final T allowed in allowedValues ) {
571
+ for (final T allowed in values ) {
545
572
if (allowed.name == data) {
546
573
return allowed;
547
574
}
548
575
}
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 ()}' );
549
580
}
550
581
return _defaultValue;
551
582
}
@@ -573,26 +604,43 @@ class RestorableEnum<T extends Enum> extends RestorableValue<T> {
573
604
/// Creates a [RestorableEnum] .
574
605
///
575
606
/// {@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 ();
578
610
579
611
@override
580
612
T createDefaultValue () => _defaultValue;
581
613
final T _defaultValue;
582
614
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.
584
626
///
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.
588
629
///
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 .
591
632
///
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;
596
644
597
645
@override
598
646
void didUpdateValue (T ? oldValue) {
@@ -602,11 +650,15 @@ class RestorableEnum<T extends Enum> extends RestorableValue<T> {
602
650
@override
603
651
T fromPrimitives (Object ? data) {
604
652
if (data != null && data is String ) {
605
- for (final T allowed in allowedValues ) {
653
+ for (final T allowed in values ) {
606
654
if (allowed.name == data) {
607
655
return allowed;
608
656
}
609
657
}
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 ()}' );
610
662
}
611
663
return _defaultValue;
612
664
}
0 commit comments