From 97c1f0b7b2667b158157b2a3465bd3555913c7be Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Tue, 21 Jun 2016 14:10:13 -0700 Subject: [PATCH] Route.didMount should throw a useful error message (#4665) The error code path wasn't actually getting the point where it would throw the useful error message. Also, fix error recovery when runApp fails. Fixes #4655 --- packages/flutter/lib/src/widgets/binding.dart | 20 +++++++++++++++++-- packages/flutter/lib/src/widgets/routes.dart | 19 ++++++++++-------- .../flutter/test/widget/navigator_test.dart | 12 +++++++++++ 3 files changed, 41 insertions(+), 10 deletions(-) diff --git a/packages/flutter/lib/src/widgets/binding.dart b/packages/flutter/lib/src/widgets/binding.dart index 2fc161a639..e2c98c1736 100644 --- a/packages/flutter/lib/src/widgets/binding.dart +++ b/packages/flutter/lib/src/widgets/binding.dart @@ -349,14 +349,30 @@ class RenderObjectToWidgetElement extends RootRenderObje void mount(Element parent, dynamic newSlot) { assert(parent == null); super.mount(parent, newSlot); - _child = updateChild(_child, widget.child, _rootChildSlot); + _rebuild(); } @override void update(RenderObjectToWidgetAdapter newWidget) { super.update(newWidget); assert(widget == newWidget); - _child = updateChild(_child, widget.child, _rootChildSlot); + _rebuild(); + } + + void _rebuild() { + try { + _child = updateChild(_child, widget.child, _rootChildSlot); + assert(_child != null); + } catch (exception, stack) { + FlutterError.reportError(new FlutterErrorDetails( + exception: exception, + stack: stack, + library: 'widgets library', + context: 'attaching to the render tree' + )); + Widget error = new ErrorWidget(exception); + _child = updateChild(null, error, _rootChildSlot); + } } @override diff --git a/packages/flutter/lib/src/widgets/routes.dart b/packages/flutter/lib/src/widgets/routes.dart index 01de9f0af8..8fc2b429f9 100644 --- a/packages/flutter/lib/src/widgets/routes.dart +++ b/packages/flutter/lib/src/widgets/routes.dart @@ -525,14 +525,17 @@ abstract class ModalRoute extends TransitionRoute with LocalHistoryRoute()); expect('$exception', startsWith('openTransaction called with a context')); }); + + testWidgets('Missing settings in onGenerateRoute throws exception', (WidgetTester tester) async { + await tester.pumpWidget(new Navigator( + onGenerateRoute: (RouteSettings settings) { + return new MaterialPageRoute( + builder: (BuildContext context) => new Container() + ); + } + )); + Object exception = tester.takeException(); + expect(exception is FlutterError, isTrue); + }); }