Repository: flutter
Issue: [go_router] GoRouteData object recreated multiple times during navigation execution
Labels: package,has reproducible steps,P2,p: go_router_builder,found in release: 3.7,found in release: 3.11,team-go_router,triaged-go_router

### 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

To reproduce the problem simply perform a navigation (both `go` and `push` method reproduce the problem)

### Expected results

As my understanding from the code comment, the intention is to preserve the same `GoRouteData` instance to improve performances, so the expected result is not to create additional ones for redirect and build executions.

### Actual results

Since [PR #3005](https://github.com/flutter/packages/pull/3005) everytime a go or push is performed, the `GoRouteData` object is created 3 times:

* when performing the navigation `ex.: MyGoRouteData().go(context)`
* when the `GoRouteData` redirect function is called
* when the `GoRouteData` build or pageBuilder functions are called

In the previous implementation the `GoRouteData` itself was passed as the extra parameter, thus being returned by the `T factoryImpl(GoRouterState state)` function in `GoRouteData.$route` static method and mitigating the problem.

In the current implementation an `Expando` on `GoRouterState` is used to cache the `GoRouteData` instance, but the `GoRouterState` instance is recreated each time `$route.build`, `$route.pageBuilder` or `$route.redirect` are called resulting in multiple calls of the `factory(state)` instruction.


### Code sample


<details>
  <summary>main.dart</summary>

```dart
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';

part 'main.g.dart';

void main() {
  runApp(const MainApp());
}

final router = GoRouter(
  debugLogDiagnostics: true,
  routes: $appRoutes,
);

@TypedGoRoute<RootPageData>(
  path: '/',
  routes: [
    TypedGoRoute<LalleroPageData>(
      path: 'lallero',
    )
  ],
)
class RootPageData extends GoRouteData {
  RootPageData() {
    debugPrint('root page $hashCode');
  }

  @override
  Widget build(BuildContext context, GoRouterState state) => Scaffold(
        appBar: AppBar(),
        body: Center(
          child: ElevatedButton(
            child: const Text('Go to Lallero Page'),
            onPressed: () => LalleroPageData().go(context),
          ),
        ),
      );
}

class LalleroPageData extends GoRouteData {
  LalleroPageData() {
    debugPrint('lallero page $hashCode');
  }

  @override
  Widget build(BuildContext context, GoRouterState state) => Scaffold(
        appBar: AppBar(),
        body: Center(
          child: ElevatedButton(
            child: const Text('Go to Root Page'),
            onPressed: () => RootPageData().go(context),
          ),
        ),
      );
}

class MainApp extends StatelessWidget {
  const MainApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp.router(
      routerConfig: router,
    );
  }
}

```

</details>

<details>
  <summary>main.g.dart</summary>

```dart
// GENERATED CODE - DO NOT MODIFY BY HAND

part of 'main.dart';

// **************************************************************************
// GoRouterGenerator
// **************************************************************************

List<RouteBase> get $appRoutes => [
      $rootPageData,
    ];

RouteBase get $rootPageData => GoRouteData.$route(
      path: '/',
      factory: $RootPageDataExtension._fromState,
      routes: [
        GoRouteData.$route(
          path: 'lallero',
          factory: $LalleroPageDataExtension._fromState,
        ),
      ],
    );

extension $RootPageDataExtension on RootPageData {
  static RootPageData _fromState(GoRouterState state) => RootPageData();

  String get location => GoRouteData.$location(
        '/',
      );

  void go(BuildContext context) => context.go(location);

  Future<T?> push<T>(BuildContext context) => context.push<T>(location);

  void pushReplacement(BuildContext context) =>
      context.pushReplacement(location);
}

extension $LalleroPageDataExtension on LalleroPageData {
  static LalleroPageData _fromState(GoRouterState state) => LalleroPageData();

  String get location => GoRouteData.$location(
        '/lallero',
      );

  void go(BuildContext context) => context.go(location);

  Future<T?> push<T>(BuildContext context) => context.push<T>(location);

  void pushReplacement(BuildContext context) =>
      context.pushReplacement(location);
}
```

</details>


### Screenshots or Video

_No response_

### Logs

<details><summary>Logs</summary>

```console
Launching lib/main.dart on sdk gphone64 arm64 in debug mode...
main.dart:1
✓  Built build/app/outputs/flutter-apk/app-debug.apk.
Connecting to VM Service at ws://127.0.0.1:61049/kcWYLQhE_WQ=/ws
[GoRouter] Full paths for routes:
             => /
             =>   /lallero
[GoRouter] setting initial location null
I/flutter (13439): root page 116147713
I/flutter (13439): root page 76135971
[GoRouter] Using MaterialApp configuration
I/router_data_bug(13439): Compiler allocated 4579KB to compile void android.view.ViewRootImpl.performTraversals()
E/SurfaceSyncer(13439): Failed to find sync for id=0
W/Parcel  (13439): Expecting binder but got null!
I/flutter (13439): lallero page 693854106
D/EGL_emulation(13439): app_time_stats: avg=1016.46ms min=5.49ms max=2027.43ms count=2
[GoRouter] going to /lallero
I/flutter (13439): root page 736197803
I/flutter (13439): lallero page 1047984655
I/flutter (13439): root page 1017012617
```

</details>


### Flutter Doctor output

<details><summary>Doctor output</summary>

```console
❯ fflutter doctor -v
[!] Flutter (Channel stable, 3.7.12, on macOS 13.3.1 22E261 darwin-arm64, locale scn-IT)
    • Flutter version 3.7.12 on channel stable at /Users/mmucaria/fvm/versions/stable
    ! Warning: `flutter` on your path resolves to /Library/Developer/mmucaria/flutter/bin/flutter, which is not inside your current Flutter SDK checkout at /Users/mmucaria/fvm/versions/stable. Consider adding /Users/mmucaria/fvm/versions/stable/bin to the front of your path.
    ! Warning: `dart` on your path resolves to /Library/Developer/mmucaria/flutter/bin/dart, which is not inside your current Flutter SDK checkout at /Users/mmucaria/fvm/versions/stable. Consider adding /Users/mmucaria/fvm/versions/stable/bin to the front of your path.
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 4d9e56e694 (2 weeks ago), 2023-04-17 21:47:46 -0400
    • Engine revision 1a65d409c7
    • Dart version 2.19.6
    • DevTools version 2.20.1
    • 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 33.0.0)
    • Android SDK at /Users/mmucaria/Library/Android/sdk
    • Platform android-33, build-tools 33.0.0
    • ANDROID_HOME = /Users/mmucaria/Library/Android/sdk
    • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.15+0-b2043.56-8887301)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 14.2)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 14C18
    • CocoaPods version 1.12.0

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2022.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.15+0-b2043.56-8887301)

[✓] VS Code (version 1.77.3)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.62.0

[✓] Connected device (3 available)
    • sdk gphone64 arm64 (mobile) • emulator-5554 • android-arm64  • Android 13 (API 33) (emulator)
    • macOS (desktop)             • macos         • darwin-arm64   • macOS 13.3.1 22E261 darwin-arm64
    • Chrome (web)                • chrome        • web-javascript • Google Chrome 113.0.5672.63

[✓] HTTP Host Availability
    • All required HTTP hosts are available

! Doctor found issues in 1 category.
```

</details>