Repository: flutter
Issue: HeroController occasionally crashes, when pages managed by Navigator
Labels: c: crash,framework,f: routes,a: production,P2,needs repro info,team-framework,triaged-framework

### Is there an existing issue for this?

- [X] I have searched the [existing issues](https://github.com/flutter/flutter/issues)
- [X] I have read the [guide to filing a bug](https://flutter.dev/docs/resources/bug-reports)

### Steps to reproduce

We found that the number of `HeroController` crashes with the following stack is relatively large in monitoring data, but we failed to reproduce it in our attempts. I will provide an executable demo later, but it still cannot be reproduced stably. Please see if the following description helps solve the problem.
The ways to use `Navigator`: 
1. At first, there is only one page in `pages` of `Navigator`. On this basis, the user can exit this page or continue to open a new page.
2. Set a set of pages in `Navigator`'s `pages` during initialization. On this basis, the user can exit the page or continue to open a new page.
3. At first, there is only one page in `Navigator`'s `pages`, if the user triggers back, it does not exit `Navigator`, but replaces the page with a new page.

### Expected results

`HeroController` works fine.

### Actual results

The crash stack trace:
```
#0 RenderBox.size (/Volumes/data/workspace/.sdk/flutter/packages/flutter/lib/src/rendering/box.dart:1988:17)
#1 _HeroFlightManifest._boundingBoxFor (/Volumes/data/workspace/.sdk/flutter/packages/flutter/lib/src/widgets/heroes.dart:480:25)
#2 _HeroFlightManifest.toHeroLocation (/Volumes/data/workspace/.sdk/flutter/packages/flutter/lib/src/widgets/heroes.dart:493:36)
#3 _HeroFlightManifest.isValid (/Volumes/data/workspace/.sdk/flutter/packages/flutter/lib/src/widgets/heroes.dart)
#4 HeroController._startHeroTransition (/Volumes/data/workspace/.sdk/flutter/packages/flutter/lib/src/widgets/heroes.dart)
#5 HeroController._maybeStartHeroTransition.<anonymous closure> (/Volumes/data/workspace/.sdk/flutter/packages/flutter/lib/src/widgets/heroes.dart:883:11)
#6 SchedulerBinding._invokeFrameCallback (/Volumes/data/workspace/.sdk/flutter/packages/flutter/lib/src/scheduler/binding.dart:1284:15)
#7 SchedulerBinding.handleDrawFrame (/Volumes/data/workspace/.sdk/flutter/packages/flutter/lib/src/scheduler/binding.dart:1223:9)
#8 SchedulerBinding._handleDrawFrame (/Volumes/data/workspace/.sdk/flutter/packages/flutter/lib/src/scheduler/binding.dart:1072:5)
#9 SchedulerBinding._handleDrawFrame (/Volumes/data/workspace/.sdk/flutter/packages/flutter/lib/src/scheduler/binding.dart:1054:3)
#10 _rootRun (third_party/dart/sdk/lib/async/zone.dart:1399:13)
#11 _rootRun (third_party/dart/sdk/lib/async/zone.dart:1390:1)
#12 _CustomZone.run (third_party/dart/sdk/lib/async/zone.dart:1301:19)
```

### Code sample

<details><summary>Code sample</summary>

```dart
/// the simplified demo of how we use Navigator
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(
    MaterialApp(
      home: const MyApp(),
    ),
  );
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      home: Scaffold(
        appBar: AppBar(
          title: Text('Navigator test'),
        ),
        body: Center(
          child: MaterialButton(
            child: const Text('Go'),
            onPressed: () {
              Navigator.of(context).push(
                CupertinoPageRoute(
                  builder: (_) => MyHomePage(),
                ),
              );
            },
          ),
        ),
      ),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final _navigatorKey = GlobalKey<NavigatorState>();

  final _pages = <Page>[
    MyPage(
      key: ValueKey('/page/a'),
      name: '/page/a',
      child: PageA(),
    ),
    MyPage(
      key: ValueKey('/page/b0'),
      name: '/page/b0',
      child: PageB(
        index: 0,
      ),
    ),
    MyPage(
      key: ValueKey('/page/b1'),
      name: '/page/b1',
      child: PageB(
        index: 1,
      ),
    ),
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: WillPopScope(
        onWillPop: () async => (await _navigatorKey.currentState?.maybePop()) == false,
        child: Navigator(
          key: _navigatorKey,
          pages: _pages,
          onPopPage: _onPopPage,
        ),
      ),
    );
  }

  bool _onPopPage(Route<dynamic> route, dynamic result) {
    if (_pages.isNotEmpty) {
      _pages.removeLast();
      setState(() {});
    }
    return route.didPop(result);
  }
}

class PageA extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('A'),
      ),
      body: Center(
        child: Text(
          'AA',
        ),
      ),
    );
  }
}

class PageB extends StatelessWidget {
  const PageB({super.key, required this.index});

  final int index;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('B'),
      ),
      body: Center(
        child: Text(
          'B$index',
        ),
      ),
    );
  }
}

class MyPage<T> extends Page<T> {
  const MyPage({
    required LocalKey key,
    required String name,
    required this.child,
  }) : super(key: key, name: name);

  final Widget child;

  @override
  Route<T> createRoute(BuildContext context) {
    return CupertinoPageRoute(
      settings: this,
      builder: (context) => child,
    );
  }

  @override
  String toString() => '$name';
}

```
</details>


### Screenshots or Video

_No response_

### Logs

_No response_

### Flutter Doctor output

<details><summary>Doctor output</summary>

```console
[✓] Flutter (Channel unknown, 3.10.7, on macOS 13.5 22G74 darwin-arm64, locale zh-Hans-CN)
    ! Flutter version 3.10.7 on channel unknown at /Users/x/proj/lib/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision e285328a69 (8 weeks ago), 2023-08-17 17:55:17 -0700
    • Engine revision 077a732ef4
    • Dart version 3.0.7
    • DevTools version 2.23.1
    • Pub download mirror https://pub.flutter-io.cn
    • Flutter download mirror https://storage.flutter-io.cn
    • If those were intentional, you can disregard the above warnings; however it is recommended to use "git" directly to perform update checks and upgrades.

[!] Android toolchain - develop for Android devices (Android SDK version 32.1.0-rc1)
    • Android SDK at /Users/x/Library/Android/sdk
    ✗ cmdline-tools component is missing
      Run `path/to/sdkmanager --install "cmdline-tools;latest"`
      See https://developer.android.com/studio/command-line for more details.
    ✗ Android license status unknown.
      Run `flutter doctor --android-licenses` to accept the SDK licenses.
      See https://flutter.dev/docs/get-started/install/macos#android-setup for more details.

[✓] Xcode - develop for iOS and macOS (Xcode 14.3)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 14E222b
    • CocoaPods version 1.11.3

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2022.3)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b829.9-10027231)

[✓] IntelliJ IDEA Ultimate Edition (version 2022.1)
    • IntelliJ at /Applications/IntelliJ IDEA.app
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin version 221.5588

[✓] VS Code (version 1.79.2)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.74.0

[!] Proxy Configuration

[✓] Connected device (4 available)

[✓] Network resources
    • All expected network resources are available.
```

</details>