From 248645a2b2c9baac4603626d34193a5d0d712c51 Mon Sep 17 00:00:00 2001 From: Polina Cherkasova Date: Fri, 1 Sep 2023 17:59:25 -0700 Subject: [PATCH] RestorableProperty should dispatch creation in constructor. (#133883) --- packages/flutter/lib/src/widgets/restoration.dart | 7 +++++++ packages/flutter/test/material/time_test.dart | 4 +++- .../flutter/test/widgets/restorable_property_test.dart | 5 +++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/packages/flutter/lib/src/widgets/restoration.dart b/packages/flutter/lib/src/widgets/restoration.dart index 101e6422da..367d90e47e 100644 --- a/packages/flutter/lib/src/widgets/restoration.dart +++ b/packages/flutter/lib/src/widgets/restoration.dart @@ -454,6 +454,13 @@ class _RootRestorationScopeState extends State { /// * [RestorationManager], which describes how state restoration works in /// Flutter. abstract class RestorableProperty extends ChangeNotifier { + /// Creates a [RestorableProperty]. + RestorableProperty(){ + if (kFlutterMemoryAllocationsEnabled) { + maybeDispatchObjectCreation(); + } + } + /// Called by the [RestorationMixin] if no restoration data is available to /// restore the value of the property from to obtain the default value for the /// property. diff --git a/packages/flutter/test/material/time_test.dart b/packages/flutter/test/material/time_test.dart index 6c71fef456..2c9926ac17 100644 --- a/packages/flutter/test/material/time_test.dart +++ b/packages/flutter/test/material/time_test.dart @@ -58,7 +58,9 @@ void main() { group('RestorableTimeOfDay tests', () { testWidgetsWithLeakTracking('value is not accessible when not registered', (WidgetTester tester) async { - expect(() => RestorableTimeOfDay(const TimeOfDay(hour: 20, minute: 4)).value, throwsAssertionError); + final RestorableTimeOfDay property = RestorableTimeOfDay(const TimeOfDay(hour: 20, minute: 4)); + addTearDown(() => property.dispose()); + expect(() => property.value, throwsAssertionError); }); testWidgets('work when not in restoration scope', (WidgetTester tester) async { diff --git a/packages/flutter/test/widgets/restorable_property_test.dart b/packages/flutter/test/widgets/restorable_property_test.dart index ef33e0f5eb..bb985cd130 100644 --- a/packages/flutter/test/widgets/restorable_property_test.dart +++ b/packages/flutter/test/widgets/restorable_property_test.dart @@ -4,6 +4,7 @@ import 'package:flutter/widgets.dart'; import 'package:flutter_test/flutter_test.dart'; +import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart'; void main() { testWidgets('value is not accessible when not registered', (WidgetTester tester) async { @@ -25,6 +26,10 @@ void main() { expect(() => _TestRestorableValue().value, throwsAssertionError); }); + testWidgetsWithLeakTracking('$RestorableProperty dispatches creation in constructor', (WidgetTester widgetTester) async { + expect(()=> RestorableDateTimeN(null).dispose(), dispatchesMemoryEvents(RestorableDateTimeN)); + }); + testWidgets('work when not in restoration scope', (WidgetTester tester) async { await tester.pumpWidget(const _RestorableWidget());