From 2eee5ae949f5c1b3919cc9a9097e97259599ab88 Mon Sep 17 00:00:00 2001 From: Shi-Hao Hong Date: Wed, 16 Dec 2020 09:53:04 +0800 Subject: [PATCH] [State Restoration] RestorableBoolN (#71653) --- .../src/widgets/restoration_properties.dart | 74 +++++++++++++------ .../widgets/restorable_property_test.dart | 9 ++- 2 files changed, 58 insertions(+), 25 deletions(-) diff --git a/packages/flutter/lib/src/widgets/restoration_properties.dart b/packages/flutter/lib/src/widgets/restoration_properties.dart index b1924d2ce6..a4ba8dfe7e 100644 --- a/packages/flutter/lib/src/widgets/restoration_properties.dart +++ b/packages/flutter/lib/src/widgets/restoration_properties.dart @@ -143,19 +143,11 @@ abstract class RestorableValue extends RestorableProperty { void didUpdateValue(T? oldValue); } -// _RestorablePrimitiveValue and its subclasses do not allow null values in -// anticipation of NNBD (non-nullability by default). -// -// If necessary, we can in the future define a new subclass hierarchy that -// does allow null values for primitive types. Borrowing from lisp where -// functions that returned a bool ended in 'p', a suggested naming scheme for -// these new subclasses could be to add 'N' (for nullable) to the end of a -// class name (e.g. RestorableIntN, RestorableStringN, etc.) to distinguish them -// from their non-nullable friends. -class _RestorablePrimitiveValue extends RestorableValue { - _RestorablePrimitiveValue(this._defaultValue) - : assert(_defaultValue != null), - assert(debugIsSerializableForRestoration(_defaultValue)), +// _RestorablePrimitiveValueN and its subclasses allows for null values. +// See [_RestorablePrimitiveValue] for the non-nullable version of this class. +class _RestorablePrimitiveValueN extends RestorableValue { + _RestorablePrimitiveValueN(this._defaultValue) + : assert(debugIsSerializableForRestoration(_defaultValue)), super(); final T _defaultValue; @@ -163,28 +155,43 @@ class _RestorablePrimitiveValue extends RestorableValue { @override T createDefaultValue() => _defaultValue; - @override - set value(T value) { - assert(value != null); - super.value = value; - } - @override void didUpdateValue(T? oldValue) { assert(debugIsSerializableForRestoration(value)); notifyListeners(); } + @override + T fromPrimitives(Object? serialized) => serialized as T; + + @override + Object? toPrimitives() => value; +} + +// _RestorablePrimitiveValue and its subclasses are non-nullable. +// See [_RestorablePrimitiveValueN] for the nullable version of this class. +class _RestorablePrimitiveValue extends _RestorablePrimitiveValueN { + _RestorablePrimitiveValue(T _defaultValue) + : assert(_defaultValue != null), + assert(debugIsSerializableForRestoration(_defaultValue)), + super(_defaultValue); + + @override + set value(T value) { + assert(value != null); + super.value = value; + } + @override T fromPrimitives(Object? serialized) { assert(serialized != null); - return serialized! as T; + return super.fromPrimitives(serialized); } @override Object toPrimitives() { assert(value != null); - return value; + return super.toPrimitives()!; } } @@ -249,9 +256,28 @@ class RestorableBool extends _RestorablePrimitiveValue { /// Creates a [RestorableBool]. /// /// {@macro flutter.widgets.RestorableNum.constructor} + /// + /// See also: + /// + /// * [RestorableBoolN] for the nullable version of this class. RestorableBool(bool defaultValue) : assert(defaultValue != null), super(defaultValue); } +/// A [RestorableProperty] that knows how to store and restore a [bool] that is +/// nullable. +/// +/// {@macro flutter.widgets.RestorableNum} +class RestorableBoolN extends _RestorablePrimitiveValueN { + /// Creates a [RestorableBoolN]. + /// + /// {@macro flutter.widgets.RestorableNum.constructor} + /// + /// See also: + /// + /// * [RestorableBool] for the non-nullable version of this class. + RestorableBoolN(bool? defaultValue) : super(defaultValue); +} + /// A base class for creating a [RestorableProperty] that stores and restores a /// [Listenable]. /// @@ -313,17 +339,17 @@ abstract class RestorableListenable extends RestorableProp abstract class RestorableChangeNotifier extends RestorableListenable { @override void initWithValue(T value) { - _diposeOldValue(); + _disposeOldValue(); super.initWithValue(value); } @override void dispose() { - _diposeOldValue(); + _disposeOldValue(); super.dispose(); } - void _diposeOldValue() { + void _disposeOldValue() { if (_value != null) { // Scheduling a microtask for dispose to give other entities a chance // to remove their listeners first. diff --git a/packages/flutter/test/widgets/restorable_property_test.dart b/packages/flutter/test/widgets/restorable_property_test.dart index 85a10de0f8..a605bb52f3 100644 --- a/packages/flutter/test/widgets/restorable_property_test.dart +++ b/packages/flutter/test/widgets/restorable_property_test.dart @@ -13,6 +13,7 @@ void main() { expect(() => RestorableInt(1).value, throwsAssertionError); expect(() => RestorableString('hello').value, throwsAssertionError); expect(() => RestorableBool(true).value, throwsAssertionError); + expect(() => RestorableBoolN(true).value, throwsAssertionError); expect(() => RestorableTextEditingController().value, throwsAssertionError); expect(() => _TestRestorableValue().value, throwsAssertionError); }); @@ -125,6 +126,7 @@ void main() { state.intValue.value = 10; state.stringValue.value = 'guten tag'; state.boolValue.value = true; + state.nullableBoolValue.value = false; state.controllerValue.value.text = 'blabla'; state.objectValue.value = 53; }); @@ -140,6 +142,7 @@ void main() { state.intValue.value = 20; state.stringValue.value = 'ciao'; state.boolValue.value = false; + state.nullableBoolValue.value = null; state.controllerValue.value.text = 'blub'; state.objectValue.value = 20; }); @@ -154,6 +157,7 @@ void main() { expect(state.intValue.value, 10); expect(state.stringValue.value, 'guten tag'); expect(state.boolValue.value, true); + expect(state.nullableBoolValue.value, false); expect(state.controllerValue.value.text, 'blabla'); expect(state.objectValue.value, 53); expect(find.text('guten tag'), findsOneWidget); @@ -166,6 +170,7 @@ void main() { expect(state.intValue.value, 42); expect(state.stringValue.value, 'hello world'); expect(state.boolValue.value, false); + expect(state.nullableBoolValue.value, null); expect(state.controllerValue.value.text, 'FooBar'); expect(state.objectValue.value, 55); expect(find.text('hello world'), findsOneWidget); @@ -323,6 +328,7 @@ class _RestorableWidgetState extends State<_RestorableWidget> with RestorationMi final RestorableInt intValue = RestorableInt(42); final RestorableString stringValue = RestorableString('hello world'); final RestorableBool boolValue = RestorableBool(false); + final RestorableBoolN nullableBoolValue = RestorableBoolN(null); final RestorableTextEditingController controllerValue = RestorableTextEditingController(text: 'FooBar'); final _TestRestorableValue objectValue = _TestRestorableValue(); @@ -332,7 +338,8 @@ class _RestorableWidgetState extends State<_RestorableWidget> with RestorationMi registerForRestoration(doubleValue, 'double'); registerForRestoration(intValue, 'int'); registerForRestoration(stringValue, 'string'); - registerForRestoration(boolValue,'bool'); + registerForRestoration(boolValue, 'bool'); + registerForRestoration(nullableBoolValue, 'nullableBool'); registerForRestoration(controllerValue, 'controller'); registerForRestoration(objectValue, 'object'); }