Repository: flutter
Issue: When there is a RouteObserver set inside the child of a Hero widget, Flutter crashes
Labels: c: crash,framework,f: routes,has reproducible steps,P2,found in release: 3.3,found in release: 3.6,team-framework,triaged-framework

If you take out only the Hero widget from the below code, everything works as expected.

## Code
``` Dart
import 'package:flutter/material.dart';
import 'test_page.dart';
import 'test_widget.dart';

final RouteObserver<PageRoute> routeObserver = RouteObserver<PageRoute>();

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'HERO and RouteObserver'),
      routes: <String, WidgetBuilder> {
        'test': (BuildContext context) => TestPage(
            routeObserver: routeObserver
        )
      },
      navigatorObservers: [routeObserver],
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.blue,
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Hero(
        tag: "HeroTag",
        child: TestWidget(
          routeObserver: routeObserver
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _toTestPage,
        child: Icon(Icons.add),
      ),
    );
  }

  void _toTestPage() {
    Navigator.of(context).pushNamed("test");
  }
}
```
``` Dart
import 'package:flutter/material.dart';
import 'test_widget.dart';

class TestPage extends StatefulWidget {
  const TestPage({
    this.routeObserver
  });

  final RouteObserver<PageRoute> routeObserver;

  @override
  State<StatefulWidget> createState() => TestPageState();
}

class TestPageState extends State<TestPage> with RouteAware {
  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    widget.routeObserver.subscribe(this, ModalRoute.of(context));
  }

  @override
  void didPush() {
    print("did push: test_page");
  }

  @override
  void didPushNext() {
    print("did push next: test_page");
  }

  @override
  void didPop() {
    print("did pop: test_page");
  }

  @override
  void didPopNext() {
    print("did pop next: test_page");
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.blue,
      appBar: AppBar(
        title: Text("Test Page"),
      ),
      body: Center(
        child: Container(
          width: 200,
          height: 200,
          child: Hero(
            tag: "HeroTag",
            child: TestWidget(
              routeObserver: widget.routeObserver
            ),
          ),
        ),
      ),
    );
  }
}
```
``` Dart
import 'package:flutter/material.dart';

class TestWidget extends StatefulWidget {
  const TestWidget({
    this.routeObserver
  });

  final RouteObserver<PageRoute> routeObserver;

  @override
  State<StatefulWidget> createState() => TestWidgetState();
}

class TestWidgetState extends State<TestWidget> with RouteAware {
  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    widget.routeObserver.subscribe(this, ModalRoute.of(context));
  }

  @override
  void didPush() {
    print("did push: test_widget");
  }

  @override
  void didPushNext() {
    print("did push next: test_widget");
  }

  @override
  void didPop() {
    print("did pop: test_widget");
  }

  @override
  void didPopNext() {
    print("did pop next: test_widget");
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.red,
      child : Center(
        child: Text("Hero"),
      )
    );
  }
}
```

## Logs
```
[+1116 ms] I/flutter (18702): did push next: test_widget
[  +26 ms] I/flutter (18702): did push: test_page
[   +3 ms] I/flutter (18702): did push: test_widget
[  +96 ms] I/flutter (18702): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
[   +2 ms] I/flutter (18702): The following assertion was thrown building Positioned(left: 65.2, top: 171.0, right: 65.2, bottom:
[   +1 ms] I/flutter (18702): 91.0):
[   +1 ms] I/flutter (18702): 'package:flutter/src/widgets/routes.dart': Failed assertion: line 1369 pos 12: 'route != null': is
[   +1 ms] I/flutter (18702): not true.
[        ] I/flutter (18702): 
[        ] I/flutter (18702): Either the assertion indicates an error in the framework itself, or we should provide substantially
[        ] I/flutter (18702): more information in this error message to help you determine and fix the underlying cause.
[        ] I/flutter (18702): In either case, please report this assertion by filing a bug on GitHub:
[        ] I/flutter (18702):   https://github.com/flutter/flutter/issues/new?template=BUG.md
[        ] I/flutter (18702): 
[        ] I/flutter (18702): When the exception was thrown, this was the stack:
[        ] I/flutter (18702): #2      RouteObserver.subscribe (package:flutter/src/widgets/routes.dart:1369:12)
[        ] I/flutter (18702): #3      TestWidgetState.didChangeDependencies (package:hero_error/test_widget.dart:28:26)
[        ] I/flutter (18702): #4      StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3862:12)
[        ] I/flutter (18702): #5      ComponentElement.mount (package:flutter/src/widgets/framework.dart:3711:5)
[        ] I/flutter (18702): #6      Element.inflateWidget (package:flutter/src/widgets/framework.dart:2956:14)
[        ] I/flutter (18702): #7      Element.updateChild (package:flutter/src/widgets/framework.dart:2759:12)
[        ] I/flutter (18702): #8      SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4876:14)
[        ] I/flutter (18702): #9      Element.inflateWidget (package:flutter/src/widgets/framework.dart:2956:14)
[        ] I/flutter (18702): #10     Element.updateChild (package:flutter/src/widgets/framework.dart:2759:12)
[        ] I/flutter (18702): #11     SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4876:14)
[        ] I/flutter (18702): #12     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2956:14)
[        ] I/flutter (18702): #13     Element.updateChild (package:flutter/src/widgets/framework.dart:2759:12)
[        ] I/flutter (18702): #14     SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4876:14)
[        ] I/flutter (18702): #15     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2956:14)
[        ] I/flutter (18702): #16     Element.updateChild (package:flutter/src/widgets/framework.dart:2759:12)
[        ] I/flutter (18702): #17     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3747:16)
[        ] I/flutter (18702): #18     Element.rebuild (package:flutter/src/widgets/framework.dart:3559:5)
[        ] I/flutter (18702): #19     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3716:5)
[        ] I/flutter (18702): #20     ComponentElement.mount (package:flutter/src/widgets/framework.dart:3711:5)
[        ] I/flutter (18702): #21     ParentDataElement.mount (package:flutter/src/widgets/framework.dart:4063:11)
[        ] I/flutter (18702): #22     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2956:14)
[        ] I/flutter (18702): #23     Element.updateChild (package:flutter/src/widgets/framework.dart:2759:12)
[        ] I/flutter (18702): #24     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3747:16)
[        ] I/flutter (18702): #25     Element.rebuild (package:flutter/src/widgets/framework.dart:3559:5)
[        ] I/flutter (18702): #26     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3716:5)
[        ] I/flutter (18702): #27     StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3864:11)
[        ] I/flutter (18702): #28     ComponentElement.mount (package:flutter/src/widgets/framework.dart:3711:5)
[        ] I/flutter (18702): #29     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2956:14)
[        ] I/flutter (18702): #30     Element.updateChild (package:flutter/src/widgets/framework.dart:2759:12)
[        ] I/flutter (18702): #31     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3747:16)
[        ] I/flutter (18702): #32     Element.rebuild (package:flutter/src/widgets/framework.dart:3559:5)
[        ] I/flutter (18702): #33     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3716:5)
[        ] I/flutter (18702): #34     StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3864:11)
[        ] I/flutter (18702): #35     ComponentElement.mount (package:flutter/src/widgets/framework.dart:3711:5)
[        ] I/flutter (18702): #36     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2956:14)
[        ] I/flutter (18702): #37     Element.updateChild (package:flutter/src/widgets/framework.dart:2759:12)
[        ] I/flutter (18702): #38     RenderObjectElement.updateChildren (package:flutter/src/widgets/framework.dart:4659:32)
[        ] I/flutter (18702): #39     MultiChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:4992:17)
[        ] I/flutter (18702): #40     Element.updateChild (package:flutter/src/widgets/framework.dart:2748:15)
[        ] I/flutter (18702): #41     _TheatreElement.update (package:flutter/src/widgets/overlay.dart:508:16)
[        ] I/flutter (18702): #42     Element.updateChild (package:flutter/src/widgets/framework.dart:2748:15)
[        ] I/flutter (18702): #43     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3747:16)
[        ] I/flutter (18702): #44     Element.rebuild (package:flutter/src/widgets/framework.dart:3559:5)
[        ] I/flutter (18702): #45     BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2273:33)
[        ] I/flutter (18702): #46     _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding&WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:700:20)
[        ] I/flutter (18702): #47     _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:268:5)
[        ] I/flutter (18702): #48     _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:988:15)
[        ] I/flutter (18702): #49     _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:928:9)
[        ] I/flutter (18702): #50     _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:840:5)
[        ] I/flutter (18702): #54     _invoke (dart:ui/hooks.dart:209:10)
[        ] I/flutter (18702): #55     _drawFrame (dart:ui/hooks.dart:168:3)
[        ] I/flutter (18702): (elided 5 frames from class _AssertionError and package dart:async)
[        ] I/flutter (18702): ════════════════════════════════════════════════════════════════════════════════════════════════════
[  +50 ms] I/flutter (18702): Another exception was thrown: 'package:flutter/src/widgets/routes.dart': Failed assertion: line 1369 pos 12: 'route != null': is not true.
[ +171 ms] I/flutter (18702): did push: test_widget
[+2099 ms] I/flutter (18702): did push: test_widget

```
```
Analyzing hero_error...
No issues found! (ran in 1.6s)
```
```
[√] Flutter (Channel stable, v1.2.1, on Microsoft Windows [Version 10.0.17134.590], locale ja-JP)
    • Flutter version 1.2.1 at C:\Users\z.shota.yuki\Documents\flutter
    • Framework revision 8661d8aecd (4 weeks ago), 2019-02-14 19:19:53 -0800
    • Engine revision 3757390fa4
    • Dart version 2.1.2 (build 2.1.2-dev.0.0 0a7dcf17eb)

[√] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
    • Android SDK at C:\Users\z.shota.yuki\AppData\Local\Android\sdk
    • Android NDK location not configured (optional; useful for native profiling support)
    • Platform android-28, build-tools 28.0.3
    • ANDROID_HOME = C:\Users\z.shota.yuki\AppData\Local\Android\sdk
    • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1024-b02)
    • All Android licenses accepted.

[√] Android Studio (version 3.1)
    • Android Studio at C:\Program Files\Android\Android Studio
    • Flutter plugin version 29.0.1
    • Dart plugin version 173.4700
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1024-b02)

[√] VS Code, 64-bit edition (version 1.29.1)
    • VS Code at C:\Program Files\Microsoft VS Code
    • Flutter extension version 2.21.1

[√] Connected device (1 available)
    • Pixel 3 • 8ADX0R099 • android-arm64 • Android 9 (API 28)

• No issues found!
```