diff --git a/packages/flutter/lib/src/widgets/routes.dart b/packages/flutter/lib/src/widgets/routes.dart index f0ee871815..585fd892ac 100644 --- a/packages/flutter/lib/src/widgets/routes.dart +++ b/packages/flutter/lib/src/widgets/routes.dart @@ -6,6 +6,7 @@ import 'dart:async'; import 'dart:ui' as ui; import 'package:flutter/foundation.dart'; +import 'package:flutter/scheduler.dart'; import 'basic.dart'; import 'focus_manager.dart'; @@ -606,8 +607,18 @@ mixin LocalHistoryRoute on Route { _localHistory.remove(entry); entry._owner = null; entry._notifyRemoved(); - if (_localHistory.isEmpty) - changedInternalState(); + if (_localHistory.isEmpty) { + if (SchedulerBinding.instance.schedulerPhase == SchedulerPhase.persistentCallbacks) { + // The local history might be removed as a result of disposing inactive + // elements during finalizeTree. The state is locked at this moment, and + // we can only notify state has changed in the next frame. + SchedulerBinding.instance.addPostFrameCallback((Duration duration) { + changedInternalState(); + }); + } else { + changedInternalState(); + } + } } @override diff --git a/packages/flutter/test/widgets/routes_test.dart b/packages/flutter/test/widgets/routes_test.dart index 4fc4e2b8be..686961e860 100644 --- a/packages/flutter/test/widgets/routes_test.dart +++ b/packages/flutter/test/widgets/routes_test.dart @@ -1294,6 +1294,28 @@ void main() { // It should refocus page one after pops. expect(focusNodeOnPageOne.hasFocus, isTrue); }); + + testWidgets('child with local history can be disposed', (WidgetTester tester) async { + // Regression test: https://github.com/flutter/flutter/issues/52478 + await tester.pumpWidget(MaterialApp( + home: WidgetWithLocalHistory(), + )); + + final WidgetWithLocalHistoryState state = tester.state(find.byType(WidgetWithLocalHistory)); + 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. + await tester.pumpWidget(const MaterialApp( + home: Text('dummy'), + )); + // Waits for modal route to update its internal state; + await tester.pump(); + expect(tester.takeException(), null); + }); }); } @@ -1390,3 +1412,29 @@ class _TestDialogRouteWithCustomBarrierCurve extends PopupRoute { ); } } + +class WidgetWithLocalHistory extends StatefulWidget { + @override + WidgetWithLocalHistoryState createState() => WidgetWithLocalHistoryState(); +} + +class WidgetWithLocalHistoryState extends State { + LocalHistoryEntry _localHistory; + + void addLocalHistory() { + final ModalRoute route = ModalRoute.of(context); + _localHistory = LocalHistoryEntry(); + route.addLocalHistoryEntry(_localHistory); + } + + @override + void dispose() { + super.dispose(); + _localHistory.remove(); + } + + @override + Widget build(BuildContext context) { + return const Text('dummy'); + } +}