Repository: flutter
Issue: [go_router_builder][go_router] Exception thrown when parent route has `$extra` param
Labels: package,has reproducible steps,P2,p: go_router,p: go_router_builder,found in release: 3.0,found in release: 3.1,team-go_router,triaged-go_router

The generated extensions will pass route object to `extra` and read extra by casting it directly, which may cause type casting exception. The generated `ParentRoute` extension code of the example:
``` dart
extension $ParentRouteExtension on ParentRoute {
  static ParentRoute _fromState(GoRouterState state) => ParentRoute(
        $extra: state.extra as int?, // When state.extra is [ChildRoute], it will throw exception.
      );

  String get location => GoRouteData.$location(
        '/',
      );

  void go(BuildContext context) => context.go(location, extra: this);

  void push(BuildContext context) => context.push(location, extra: this);
}
```
The example is using `go_router 4.0.0` and `go_router_builder 1.0.5`.

(Another issue found when writing this example, if I add `const` to `ParentRoute`, it will generate something like this: :'(
``` dart
extension $ParentRouteExtension on ParentRoute {
  static ParentRoute _fromState(GoRouterState state) => const ParentRoute( // Can't be `const` here
        $extra: state.extra as int?,
      );

  ...
}
```

## Steps to Reproduce

<!-- Please include full steps to reproduce so that we can reproduce the problem. -->

1. Execute `flutter pub run build_runner watch --delete-conflicting-outputs` to generate extensions
2. Execute `flutter run` on the code sample <!-- (see "Code sample" section below) -->
3. Click `Go to child page` button

**Expected results:** <!-- what did you want to see? -->
Display `ChildPage`

**Actual results:** <!-- what did you see? -->
Exception thrown.

<details>
<summary>Code sample</summary>

```dart
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';

part 'main.g.dart';

@TypedGoRoute<ParentRoute>(
  path: '/',
  routes: <TypedGoRoute<GoRouteData>>[
    TypedGoRoute<ChildRoute>(
      path: 'child',
    )
  ],
)
class ParentRoute extends GoRouteData {
  ParentRoute({this.$extra});

  final int? $extra;

  @override
  Widget build(BuildContext context) => const ParentPage();
}

class ChildRoute extends GoRouteData {
  const ChildRoute();

  @override
  Widget build(BuildContext context) => const ChildPage();
}

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  MyApp({Key? key}) : super(key: key);

  late final _router = GoRouter(
    debugLogDiagnostics: true,
    routes: $appRoutes,
  );

  @override
  Widget build(BuildContext context) {
    return MaterialApp.router(
      routeInformationParser: _router.routeInformationParser,
      routerDelegate: _router.routerDelegate,
      routeInformationProvider: _router.routeInformationProvider,
    );
  }
}

class ParentPage extends StatelessWidget {
  const ParentPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Center(
      child: ElevatedButton(
        onPressed: () => const ChildRoute().go(context),
        child: const Text('Go to child page'),
      ),
    );
  }
}

class ChildPage extends StatelessWidget {
  const ChildPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return const Center(
      child: Text('Child page'),
    );
  }
}
```

</details>

<details>
  <summary>Logs</summary>

<!--
      Run your application with `flutter run --verbose` and attach all the
      log output below between the lines with the backticks. If there is an
      exception, please see if the error message includes enough information
      to explain how to solve the issue.
-->

```
Launching lib/main.dart on Chrome in debug mode...
This app is linked to the debug service: ws://127.0.0.1:62216/VJ0Ejc_XtBE=/ws
Debug service listening on ws://127.0.0.1:62216/VJ0Ejc_XtBE=/ws
💪 Running with sound null safety 💪
Connecting to VM Service at ws://127.0.0.1:62216/VJ0Ejc_XtBE=/ws
Flutter Web Bootstrap: Programmatic
Restarted application in 983ms.
[GoRouter] known full paths for routes:
[GoRouter]   => /
[GoRouter]   =>   /child
[GoRouter] setting initial location null
[GoRouter] MaterialApp found
[GoRouter] going to /child
══╡ EXCEPTION CAUGHT BY GOROUTER ╞══════════════════════════════════════════════════════════════════
The following _Exception was thrown Exception during GoRouter navigation:
Exception: Expected a value of type 'int?', but got one of type 'ChildRoute'
When the exception was thrown, this was the stack:
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart 251:49      throw_
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart 84:3        castError
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 452:10  cast
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/types.dart 367:9        as
packages/flutter_application_1/main.g.dart 26:29                                  $36ParentRouteExtension$124_fromState
packages/go_router/src/route_data.dart 79:53                                      factoryImpl
packages/go_router/src/route_data.dart 86:28                                      pageBuilder
packages/go_router/src/go_router_delegate.dart 239:27                             getPages
dart-sdk/lib/_internal/js_dev_runtime/patch/core_patch.dart 586:14                of
dart-sdk/lib/core/iterable.dart 470:12                                            toList
packages/go_router/src/go_router_delegate.dart 113:42                             [_builder]
packages/go_router/src/go_router_delegate.dart 90:41                              build
packages/flutter/src/widgets/basic.dart 7371:48                                   build
packages/flutter/src/widgets/framework.dart 4876:22                               build
packages/flutter/src/widgets/framework.dart 4806:15                               performRebuild
packages/flutter/src/widgets/framework.dart 4529:5                                rebuild
packages/flutter/src/widgets/framework.dart 4883:5                                update
packages/flutter/src/widgets/framework.dart 3530:14                               updateChild
packages/flutter/src/widgets/framework.dart 4832:16                               performRebuild
packages/flutter/src/widgets/framework.dart 4529:5                                rebuild
packages/flutter/src/widgets/framework.dart 5154:5                                update
packages/flutter/src/widgets/framework.dart 3530:14                               updateChild
packages/flutter/src/widgets/framework.dart 4832:16                               performRebuild
packages/flutter/src/widgets/framework.dart 4529:5                                rebuild
packages/flutter/src/widgets/framework.dart 5154:5                                update
packages/flutter/src/widgets/framework.dart 3530:14                               updateChild
packages/flutter/src/widgets/framework.dart 4832:16                               performRebuild
packages/flutter/src/widgets/framework.dart 4977:11                               performRebuild
packages/flutter/src/widgets/framework.dart 4529:5                                rebuild
packages/flutter/src/widgets/framework.dart 2659:18                               buildScope
packages/flutter/src/widgets/binding.dart 891:9                                   drawFrame
packages/flutter/src/rendering/binding.dart 370:5                                 [_handlePersistentFrameCallback]
packages/flutter/src/scheduler/binding.dart 1146:15                               [_invokeFrameCallback]
packages/flutter/src/scheduler/binding.dart 1083:9                                handleDrawFrame
packages/flutter/src/scheduler/binding.dart 997:5                                 [_handleDrawFrame]
lib/_engine/engine/platform_dispatcher.dart 1090:13                               invoke
lib/_engine/engine/platform_dispatcher.dart 160:5                                 invokeOnDrawFrame
lib/_engine/engine/initialization.dart 194:45                                     <fn>
════════════════════════════════════════════════════════════════════════════════════════════════════
```

<!--
     Run `flutter analyze` and attach any output of that command below.
     If there are any analysis errors, try resolving them before filing this issue.
-->

```
Analyzing flutter_application_1...                                      
No issues found! (ran in 1.5s)
```

<!-- Finally, paste the output of running `flutter doctor -v` here. -->

```
[✓] Flutter (Channel stable, 3.0.1, on macOS 12.1 21C52 darwin-arm, locale en-HK)
    • Flutter version 3.0.1 at /Users/hei/Development/sdk/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision fb57da5f94 (4 weeks ago), 2022-05-19 15:50:29 -0700
    • Engine revision caaafc5604
    • Dart version 2.17.1
    • DevTools version 2.12.2

[✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
    • Android SDK at /Users/hei/Library/Android/sdk
    • Platform android-31, build-tools 31.0.0
    • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.11+0-b60-7772763)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 13.3)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • CocoaPods version 1.11.2

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2021.1)
    • 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 11.0.11+0-b60-7772763)

[✓] VS Code (version 1.68.1)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.42.0

[✓] Connected device (2 available)
    • macOS (desktop) • macos  • darwin-arm64   • macOS 12.1 21C52 darwin-arm
    • Chrome (web)    • chrome • web-javascript • Google Chrome 102.0.5005.115

[✓] HTTP Host Availability
    • All required HTTP hosts are available

• No issues found!
```

</details>


<!--
      Consider also attaching screenshots and/or videos to better
      illustrate the issue.

      You can upload them directly on GitHub.
      Beware that video file size is limited to 10MB.
-->