From 6f4d84cf97a79bf1c65b20cb86605f6993d08e6f Mon Sep 17 00:00:00 2001 From: chunhtai <47866232+chunhtai@users.noreply.github.com> Date: Wed, 23 Dec 2020 18:04:02 -0800 Subject: [PATCH] =?UTF-8?q?improve=20error=20message=20when=20herocontroll?= =?UTF-8?q?er=20is=20shared=20by=20multiple=20navig=E2=80=A6=20(#72904)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../flutter/lib/src/widgets/navigator.dart | 24 +++++++- .../flutter/test/widgets/navigator_test.dart | 56 +++++++++++++++++++ 2 files changed, 78 insertions(+), 2 deletions(-) diff --git a/packages/flutter/lib/src/widgets/navigator.dart b/packages/flutter/lib/src/widgets/navigator.dart index 0edd0f3737..640832ae84 100644 --- a/packages/flutter/lib/src/widgets/navigator.dart +++ b/packages/flutter/lib/src/widgets/navigator.dart @@ -3425,8 +3425,28 @@ class NavigatorState extends State with TickerProviderStateMixin, Res ServicesBinding.instance!.addPostFrameCallback((Duration timestamp) { // We only check if this navigator still owns the hero controller. if (_heroControllerFromScope == newHeroController) { - assert(_heroControllerFromScope!._navigator == this); - assert(previousOwner._heroControllerFromScope != newHeroController); + final bool hasHeroControllerOwnerShip = _heroControllerFromScope!._navigator == this; + if (!hasHeroControllerOwnerShip || + previousOwner._heroControllerFromScope == newHeroController) { + final NavigatorState otherOwner = hasHeroControllerOwnerShip + ? previousOwner + : _heroControllerFromScope!._navigator!; + FlutterError.reportError( + FlutterErrorDetails( + exception: FlutterError( + 'A HeroController can not be shared by multiple Navigators. ' + 'The Navigators that share the same HeroController are:\n' + '- $this\n' + '- $otherOwner\n' + 'Please create a HeroControllerScope for each Navigator or ' + 'use a HeroControllerScope.none to prevent subtree from ' + 'receiving a HeroController.' + ), + library: 'widget library', + stack: StackTrace.current, + ), + ); + } } }); } diff --git a/packages/flutter/test/widgets/navigator_test.dart b/packages/flutter/test/widgets/navigator_test.dart index 4c14361684..11bde97dd8 100644 --- a/packages/flutter/test/widgets/navigator_test.dart +++ b/packages/flutter/test/widgets/navigator_test.dart @@ -2489,6 +2489,62 @@ void main() { expect(tester.takeException(), isAssertionError); }); + testWidgets('hero controller throws has correct error message', (WidgetTester tester) async { + final HeroControllerSpy spy = HeroControllerSpy(); + await tester.pumpWidget( + HeroControllerScope( + controller: spy, + child: Directionality( + textDirection: TextDirection.ltr, + child: Stack( + children: [ + Navigator( + initialRoute: 'navigator1', + onGenerateRoute: (RouteSettings s) { + return MaterialPageRoute( + builder: (BuildContext c) { + return const Placeholder(); + }, + settings: s, + ); + }, + ), + Navigator( + initialRoute: 'navigator2', + onGenerateRoute: (RouteSettings s) { + return MaterialPageRoute( + builder: (BuildContext c) { + return const Placeholder(); + }, + settings: s, + ); + }, + ), + ], + ), + ), + ), + ); + + final dynamic exception = tester.takeException(); + expect(exception, isFlutterError); + final FlutterError error = exception as FlutterError; + expect( + error.toStringDeep(), + equalsIgnoringHashCodes( + 'FlutterError\n' + ' A HeroController can not be shared by multiple Navigators. The\n' + ' Navigators that share the same HeroController are:\n' + ' - NavigatorState#00000(tickers: tracking 1 ticker)\n' + ' - NavigatorState#00000(tickers: tracking 1 ticker)\n' + ' Please create a HeroControllerScope for each Navigator or use a\n' + ' HeroControllerScope.none to prevent subtree from receiving a\n' + ' HeroController.\n' + '' + ), + ); + }); + group('Page api', (){ Widget buildNavigator({ required List> pages,