RestorableProperty should dispatch creation in constructor. (#133883)

This commit is contained in:
Polina Cherkasova 2023-09-01 17:59:25 -07:00 committed by GitHub
parent a3362a9ff8
commit 248645a2b2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 1 deletions

View File

@ -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.

View File

@ -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 {

View File

@ -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());