diff --git a/packages/flutter/lib/src/widgets/restoration.dart b/packages/flutter/lib/src/widgets/restoration.dart index 0003e6f136..a09ca4a039 100644 --- a/packages/flutter/lib/src/widgets/restoration.dart +++ b/packages/flutter/lib/src/widgets/restoration.dart @@ -301,7 +301,9 @@ class _RootRestorationScopeState extends State { @override void dispose() { - ServicesBinding.instance!.restorationManager.removeListener(_replaceRootBucket); + if (_rootBucketValid) { + ServicesBinding.instance!.restorationManager.removeListener(_replaceRootBucket); + } super.dispose(); } diff --git a/packages/flutter_test/lib/src/binding.dart b/packages/flutter_test/lib/src/binding.dart index dce1080ccd..a99f32bbff 100644 --- a/packages/flutter_test/lib/src/binding.dart +++ b/packages/flutter_test/lib/src/binding.dart @@ -189,13 +189,16 @@ abstract class TestWidgetsFlutterBinding extends BindingBase final TestWindow _window; @override - TestRestorationManager get restorationManager => _restorationManager; - late TestRestorationManager _restorationManager; + TestRestorationManager get restorationManager { + _restorationManager ??= createRestorationManager(); + return _restorationManager!; + } + TestRestorationManager? _restorationManager; /// Called by the test framework at the beginning of a widget test to /// prepare the binding for the next test. void reset() { - _restorationManager = createRestorationManager(); + _restorationManager = null; resetGestureBinding(); } diff --git a/packages/flutter_test/test/bindings_restoration_test.dart b/packages/flutter_test/test/bindings_restoration_test.dart new file mode 100644 index 0000000000..bfdd2db1ba --- /dev/null +++ b/packages/flutter_test/test/bindings_restoration_test.dart @@ -0,0 +1,13 @@ +// Copyright 2014 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import 'package:flutter/services.dart'; +import 'package:flutter_test/flutter_test.dart'; + +void main() { + test('Can access restoration manager without crashing', () { + final AutomatedTestWidgetsFlutterBinding binding = AutomatedTestWidgetsFlutterBinding(); + expect(binding.restorationManager, isA()); + }); +}