Unverified Commit 2eee5ae9 authored by Shi-Hao Hong's avatar Shi-Hao Hong Committed by GitHub

[State Restoration] RestorableBoolN (#71653)

parent 824c8cd0
......@@ -143,19 +143,11 @@ abstract class RestorableValue<T> extends RestorableProperty<T> {
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<T extends Object> extends RestorableValue<T> {
_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<T extends Object?> extends RestorableValue<T> {
_RestorablePrimitiveValueN(this._defaultValue)
: assert(debugIsSerializableForRestoration(_defaultValue)),
super();
final T _defaultValue;
......@@ -163,28 +155,43 @@ class _RestorablePrimitiveValue<T extends Object> extends RestorableValue<T> {
@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<T extends Object> extends _RestorablePrimitiveValueN<T> {
_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<bool> {
/// 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<bool?> {
/// 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<T extends Listenable> extends RestorableProp
abstract class RestorableChangeNotifier<T extends ChangeNotifier> extends RestorableListenable<T> {
@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.
......
......@@ -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');
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment