From 89c8616f0e417a0be0610948d92bb7b9231b82ba Mon Sep 17 00:00:00 2001 From: Taha Tesser Date: Wed, 21 Jul 2021 00:27:46 +0300 Subject: [PATCH] fix `_owner` nullability in `routers.dart` (#84840) --- packages/flutter/lib/src/widgets/routes.dart | 2 +- .../flutter/test/widgets/routes_test.dart | 47 ++++++++++++++++++- 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/packages/flutter/lib/src/widgets/routes.dart b/packages/flutter/lib/src/widgets/routes.dart index 3094f0faca..c7669144b9 100644 --- a/packages/flutter/lib/src/widgets/routes.dart +++ b/packages/flutter/lib/src/widgets/routes.dart @@ -447,7 +447,7 @@ class LocalHistoryEntry { /// Remove this entry from the history of its associated [LocalHistoryRoute]. void remove() { - _owner!.removeLocalHistoryEntry(this); + _owner?.removeLocalHistoryEntry(this); assert(_owner == null); } diff --git a/packages/flutter/test/widgets/routes_test.dart b/packages/flutter/test/widgets/routes_test.dart index 6397fcf84b..d74dc4f7e9 100644 --- a/packages/flutter/test/widgets/routes_test.dart +++ b/packages/flutter/test/widgets/routes_test.dart @@ -1701,7 +1701,6 @@ void main() { state.addLocalHistory(); // Waits for modal route to update its internal state; await tester.pump(); - // Pumps a new widget to dispose WidgetWithLocalHistory. This should cause // it to remove the local history entry from modal route during // finalizeTree. @@ -1712,6 +1711,25 @@ void main() { await tester.pump(); expect(tester.takeException(), null); }); + + testWidgets('child with no local history can be disposed', (WidgetTester tester) async { + await tester.pumpWidget(const MaterialApp( + home: WidgetWithNoLocalHistory(), + )); + + final WidgetWithNoLocalHistoryState state = tester.state(find.byType(WidgetWithNoLocalHistory)); + state.addLocalHistory(); + // Waits for modal route to update its internal state; + await tester.pump(); + // Pumps a new widget to dispose WidgetWithNoLocalHistory. This should cause + // it to remove the local history entry from modal route during + // finalizeTree. + await tester.pumpWidget(const MaterialApp( + home: Text('dummy'), + )); + await tester.pump(); + expect(tester.takeException(), null); + }); }); testWidgets('can be dismissed with escape keyboard shortcut', (WidgetTester tester) async { @@ -1972,6 +1990,33 @@ class WidgetWithLocalHistoryState extends State { } } +class WidgetWithNoLocalHistory extends StatefulWidget { + const WidgetWithNoLocalHistory({Key? key}) : super(key: key); + + @override + WidgetWithNoLocalHistoryState createState() => WidgetWithNoLocalHistoryState(); +} + +class WidgetWithNoLocalHistoryState extends State { + late LocalHistoryEntry _localHistory; + + void addLocalHistory() { + _localHistory = LocalHistoryEntry(); + // Not calling `route.addLocalHistoryEntry` here. + } + + @override + void dispose() { + super.dispose(); + _localHistory.remove(); + } + + @override + Widget build(BuildContext context) { + return const Text('dummy'); + } +} + class TransitionDetector extends DefaultTransitionDelegate { bool hasTransition = false; @override