From efc84ab1ec5f5e7916e81586af44130bda33987a Mon Sep 17 00:00:00 2001 From: Shi-Hao Hong Date: Thu, 31 Dec 2020 10:00:55 +0800 Subject: [PATCH] Fix type issue with RestorableNumN (and its subclasses) (#72862) * Fix type issue with restorableN values --- .../src/widgets/restoration_properties.dart | 8 ++-- .../widgets/restorable_property_test.dart | 41 ++++++++++++++++++- 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/packages/flutter/lib/src/widgets/restoration_properties.dart b/packages/flutter/lib/src/widgets/restoration_properties.dart index 7e170f69a6..f065388252 100644 --- a/packages/flutter/lib/src/widgets/restoration_properties.dart +++ b/packages/flutter/lib/src/widgets/restoration_properties.dart @@ -306,11 +306,11 @@ class RestorableBoolN extends _RestorablePrimitiveValueN { /// See also: /// /// * [RestorableNum] for the non-nullable version of this class. -class RestorableNumN extends _RestorablePrimitiveValueN { +class RestorableNumN extends _RestorablePrimitiveValueN { /// Creates a [RestorableNumN]. /// /// {@macro flutter.widgets.RestorableNum.constructor} - RestorableNumN(num? defaultValue) : super(defaultValue); + RestorableNumN(T defaultValue) : super(defaultValue); } /// A [RestorableProperty] that knows how to store and restore a [double] @@ -321,7 +321,7 @@ class RestorableNumN extends _RestorablePrimitiveValueN { /// See also: /// /// * [RestorableDouble] for the non-nullable version of this class. -class RestorableDoubleN extends RestorableNumN { +class RestorableDoubleN extends RestorableNumN { /// Creates a [RestorableDoubleN]. /// /// {@macro flutter.widgets.RestorableNum.constructor} @@ -336,7 +336,7 @@ class RestorableDoubleN extends RestorableNumN { /// See also: /// /// * [RestorableInt] for the non-nullable version of this class. -class RestorableIntN extends RestorableNumN { +class RestorableIntN extends RestorableNumN { /// Creates a [RestorableIntN]. /// /// {@macro flutter.widgets.RestorableNum.constructor} diff --git a/packages/flutter/test/widgets/restorable_property_test.dart b/packages/flutter/test/widgets/restorable_property_test.dart index 028134a257..3ecf4527fe 100644 --- a/packages/flutter/test/widgets/restorable_property_test.dart +++ b/packages/flutter/test/widgets/restorable_property_test.dart @@ -13,7 +13,7 @@ void main() { expect(() => RestorableInt(1).value, throwsAssertionError); expect(() => RestorableString('hello').value, throwsAssertionError); expect(() => RestorableBool(true).value, throwsAssertionError); - expect(() => RestorableNumN(0).value, throwsAssertionError); + expect(() => RestorableNumN(0).value, throwsAssertionError); expect(() => RestorableDoubleN(1.0).value, throwsAssertionError); expect(() => RestorableIntN(1).value, throwsAssertionError); expect(() => RestorableStringN('hello').value, throwsAssertionError); @@ -308,6 +308,43 @@ void main() { }); expect(state.objectValue.didUpdateValueCallCount, 1); }); + + + testWidgets('RestorableN types are properly defined', (WidgetTester tester) async { + await tester.pumpWidget(const RootRestorationScope( + restorationId: 'root-child', + child: _RestorableWidget(), + )); + + expect(find.text('hello world'), findsOneWidget); + final _RestorableWidgetState state = tester.state(find.byType(_RestorableWidget)); + state.setProperties(() { + state.nullableIntValue.value = 24; + state.nullableDoubleValue.value = 1.5; + }); + + // The following types of asserts do not work. They pass even when the + // type of `value` is a `num` and not an `int` because `num` is a + // superclass of `int`. This test is intended to prevent a regression + // where RestorableIntN's value is of type `num?`, but it is passed into + // a function which requires an `int?` value. This resulted in Dart + // compile-time errors. + // + // expect(state.nullableIntValue.value, isA()); + // expect(state.nullableIntValue.value.runtimeType, int); + + // A function that takes a nullable int value. + void takesInt(int? value) {} + // The following would result in a Dart compile-time error if `value` is + // a `num?` instead of an `int?`. + takesInt(state.nullableIntValue.value); + + // A function that takes a nullable double value. + void takesDouble(double? value) {} + // The following would result in a Dart compile-time error if `value` is + // a `num?` instead of a `double?`. + takesDouble(state.nullableDoubleValue.value); + }); } class _TestRestorableValue extends RestorableValue { @@ -348,7 +385,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 RestorableNumN nullableNumValue = RestorableNumN(null); + final RestorableNumN nullableNumValue = RestorableNumN(null); final RestorableDoubleN nullableDoubleValue = RestorableDoubleN(null); final RestorableIntN nullableIntValue = RestorableIntN(null); final RestorableStringN nullableStringValue = RestorableStringN(null);