Skip to content

Commit

Permalink
RestorableProperty should dispatch creation in constructor. (#133883)
Browse files Browse the repository at this point in the history
  • Loading branch information
polina-c authored Sep 2, 2023
1 parent a3362a9 commit 248645a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
7 changes: 7 additions & 0 deletions packages/flutter/lib/src/widgets/restoration.dart
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,13 @@ class _RootRestorationScopeState extends State<RootRestorationScope> {
/// * [RestorationManager], which describes how state restoration works in
/// Flutter.
abstract class RestorableProperty<T> 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.
Expand Down
4 changes: 3 additions & 1 deletion packages/flutter/test/material/time_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions packages/flutter/test/widgets/restorable_property_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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());

Expand Down

0 comments on commit 248645a

Please sign in to comment.