Repository: flutter
Issue: [go_router] Allow Circular routes to add nested routes
Labels: package,c: proposal,P3,p: go_router,team-go_router,triaged-go_router

### Steps to reproduce

1. Run app
2. See the error

Go Router didn't allow circular route (A route which contain itself, recursively).

On my app, i want to use go router and only the 'go' api because i also target web.

I've an e-commerce app. My product pages display several buttons to open many other product pages. 
When I go to a product page and then I go back, I expect to go back to my previous product page. That's why I try to have a circular route...

I can't and won't use 'push' because of its web limitation and because of my actual architecture using only 'go'.

### Expected results

No stack overflow

### Actual results

Stack overflow

### Code sample

<details open><summary>Code sample</summary>

```dart
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';

void main() => runApp(const MyApp());

class ProductRoute extends GoRoute {
  ProductRoute()
      : super(
          path: 'product/:id',
          builder: (BuildContext context, GoRouterState state) {
            return ProductScreen(
              id: state.pathParameters['id']!,
            );
          },
          routes: [
            ProductRoute(),
          ],
        );
}

class HomeRoute extends GoRoute {
  HomeRoute({
    super.routes,
  }) : super(
          path: '/',
          builder: (BuildContext context, GoRouterState state) {
            return const HomeScreen();
          },
        );
}

/// The route configuration.
final GoRouter _router = GoRouter(
  routes: <RouteBase>[
    HomeRoute(
      routes: <RouteBase>[
        ProductRoute(),
      ],
    ),
  ],
);

/// The main app.
class MyApp extends StatelessWidget {
  /// Constructs a [MyApp]
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp.router(
      routerConfig: _router,
    );
  }
}

/// The home screen
class HomeScreen extends StatelessWidget {
  /// Constructs a [HomeScreen]
  const HomeScreen({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Home Screen')),
      body: Center(
        child: ElevatedButton(
          onPressed: () => context.go('/product/0'),
          child: const Text('Go to the Details screen'),
        ),
      ),
    );
  }
}

class ProductScreen extends StatelessWidget {
  const ProductScreen({
    required this.id,
    super.key,
  });

  final String id;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Details Screen')),
      body: Center(
        child: ElevatedButton(
          onPressed: () => context.go('/product/${DateTime.now().millisecondsSinceEpoch}'),
          child: Text('See similar product of $id'),
        ),
      ),
    );
  }
}
```

</details>


### Screenshots or Video

<details open>
<summary>Screenshots / Video demonstration</summary>

![image](https://github.com/user-attachments/assets/cae489f3-e982-4b05-924b-3c5d51c97ea0)

</details>


### Logs

<details open><summary>Logs</summary>

```console
======== Exception caught by widgets library =======================================================
The following StackOverflowError was thrown building MyApp(dirty):
Stack Overflow

The relevant error-causing widget was: 
  MyApp MyApp:file:///Users/earminjon/Documents/projects/untitled/lib/main.dart:4:29
When the exception was thrown, this was the stack: 
#0      new ProductRoute (package:untitled/main.dart:7:3)
#1      new ProductRoute (package:untitled/main.dart:16:13)
#2      new ProductRoute (package:untitled/main.dart:16:13)
#3      new ProductRoute (package:untitled/main.dart:16:13)
#4      new ProductRoute (package:untitled/main.dart:16:13)
#5      new ProductRoute (package:untitled/main.dart:16:13)
#6      new ProductRoute (package:untitled/main.dart:16:13)
#7      new ProductRoute (package:untitled/main.dart:16:13)
#8      new ProductRoute (package:untitled/main.dart:16:13)
#9      new ProductRoute (package:untitled/main.dart:16:13)
#10     new ProductRoute (package:untitled/main.dart:16:13)
#11     new ProductRoute (package:untitled/main.dart:16:13)
#12     new ProductRoute (package:untitled/main.dart:16:13)
#13     new ProductRoute (package:untitled/main.dart:16:13)
#14     new ProductRoute (package:untitled/main.dart:16:13)
#15     new ProductRoute (package:untitled/main.dart:16:13)
#16     new ProductRoute (package:untitled/main.dart:16:13)
#17     new ProductRoute (package:untitled/main.dart:16:13)
#18     new ProductRoute (package:untitled/main.dart:16:13)
#19     new ProductRoute (package:untitled/main.dart:16:13)
#20     new ProductRoute (package:untitled/main.dart:16:13)
#21     new ProductRoute (package:untitled/main.dart:16:13)
#22     new ProductRoute (package:untitled/main.dart:16:13)
#23     new ProductRoute (package:untitled/main.dart:16:13)
#24     new ProductRoute (package:untitled/main.dart:16:13)
#25     new ProductRoute (package:untitled/main.dart:16:13)
#26     new ProductRoute (package:untitled/main.dart:16:13)
#27     new ProductRoute (package:untitled/main.dart:16:13)
#28     new ProductRoute (package:untitled/main.dart:16:13)
#29     new ProductRoute (package:untitled/main.dart:16:13)
#30     new ProductRoute (package:untitled/main.dart:16:13)
#31     new ProductRoute (package:untitled/main.dart:16:13)
#32     new ProductRoute (package:untitled/main.dart:16:13)
#33     new ProductRoute (package:untitled/main.dart:16:13)
#34     new ProductRoute (package:untitled/main.dart:16:13)
#35     new ProductRoute (package:untitled/main.dart:16:13)
#36     new ProductRoute (package:untitled/main.dart:16:13)
#37     new ProductRoute (package:untitled/main.dart:16:13)
#38     new ProductRoute (package:untitled/main.dart:16:13)
#39     new ProductRoute (package:untitled/main.dart:16:13)
#40     new ProductRoute (package:untitled/main.dart:16:13)
#41     new ProductRoute (package:untitled/main.dart:16:13)
#42     new ProductRoute (package:untitled/main.dart:16:13)
#43     new ProductRoute (package:untitled/main.dart:16:13)
...
...
#20592  new ProductRoute (package:untitled/main.dart:16:13)
#20593  new ProductRoute (package:untitled/main.dart:16:13)
#20594  new ProductRoute (package:untitled/main.dart:16:13)
#20595  new ProductRoute (package:untitled/main.dart:16:13)
#20596  new ProductRoute (package:untitled/main.dart:16:13)
#20597  new ProductRoute (package:untitled/main.dart:16:13)
#20598  new ProductRoute (package:untitled/main.dart:16:13)
#20599  new ProductRoute (package:untitled/main.dart:16:13)
#20600  new ProductRoute (package:untitled/main.dart:16:13)
#20601  new ProductRoute (package:untitled/main.dart:16:13)
#20602  new ProductRoute (package:untitled/main.dart:16:13)
#20603  new ProductRoute (package:untitled/main.dart:16:13)
#20604  new ProductRoute (package:untitled/main.dart:16:13)
#20605  new ProductRoute (package:untitled/main.dart:16:13)
#20606  new ProductRoute (package:untitled/main.dart:16:13)
#20607  new ProductRoute (package:untitled/main.dart:16:13)
#20608  new ProductRoute (package:untitled/main.dart:16:13)
#20609  new ProductRoute (package:untitled/main.dart:16:13)
#20610  new ProductRoute (package:untitled/main.dart:16:13)
#20611  new ProductRoute (package:untitled/main.dart:16:13)
#20612  new ProductRoute (package:untitled/main.dart:16:13)
#20613  new ProductRoute (package:untitled/main.dart:16:13)
#20614  new ProductRoute (package:untitled/main.dart:16:13)
#20615  _router (package:untitled/main.dart:37:9)
#20616  _router (package:untitled/main.dart)
#20617  MyApp.build (package:untitled/main.dart:51:21)
#20618  StatelessElement.build (package:flutter/src/widgets/framework.dart:5687:49)
#20619  ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5617:15)
#20620  Element.rebuild (package:flutter/src/widgets/framework.dart:5333:7)
#20621  BuildScope._tryRebuild (package:flutter/src/widgets/framework.dart:2693:15)
#20622  BuildScope._flushDirtyElements (package:flutter/src/widgets/framework.dart:2752:11)
#20623  BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:3048:18)
#20624  WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:1162:21)
#20625  RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:468:5)
#20626  SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1397:15)
#20627  SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1318:9)
#20628  SchedulerBinding.scheduleWarmUpFrame.<anonymous closure> (package:flutter/src/scheduler/binding.dart:1040:9)
#20629  PlatformDispatcher.scheduleWarmUpFrame.<anonymous closure> (dart:ui/platform_dispatcher.dart:837:16)
#20633  _RawReceivePort._handleMessage (dart:isolate-patch/isolate_patch.dart:184:12)
(elided 3 frames from class _Timer and dart:async-patch)
====================================================================================================
Reloaded 0 libraries in 128ms (compile: 7 ms, reload: 0 ms, reassemble: 73 ms).
D/EGL_emulation( 4080): app_time_stats: avg=2565.73ms min=2565.73ms max=2565.73ms count=1
```

</details>


### Flutter Doctor output

<details open><summary>Doctor output</summary>

```console
[✓] Flutter (Channel stable, 3.24.3, on macOS 15.0.1 24A348 darwin-arm64, locale fr-FR)
    • Flutter version 3.24.3 on channel stable at /Users/earminjon/fvm/versions/3.24.3
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 2663184aa7 (3 months ago), 2024-09-11 16:27:48 -0500
    • Engine revision 36335019a8
    • Dart version 3.5.3
    • DevTools version 2.37.3

[✓] Android toolchain - develop for Android devices (Android SDK version 35.0.0)
    • Android SDK at /Users/earminjon/Library/Android/Sdk
    • Platform android-35, build-tools 35.0.0
    • ANDROID_HOME = /Users/earminjon/Library/Android/Sdk
    • Java binary at: /Users/earminjon/Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.11+0-17.0.11b1207.24-11852314)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 16.0)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 16A242d
    • CocoaPods version 1.15.2

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2024.1)
    • Android Studio at /Users/earminjon/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.11+0-17.0.11b1207.24-11852314)

[✓] Android Studio (version 2023.2)
    • Android Studio at /Users/perso/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.9+0-17.0.9b1087.7-11185874)

[✓] IntelliJ IDEA Ultimate Edition (version 2024.2.3)
    • IntelliJ at /Users/earminjon/Applications/IntelliJ IDEA Ultimate.app
    • Flutter plugin version 83.0.3
    • Dart plugin version 242.24931

[✓] Connected device (4 available)
    • sdk gphone64 arm64 (mobile)     • emulator-5554         • android-arm64  • Android 15 (API 35) (emulator)
    • macOS (desktop)                 • macos                 • darwin-arm64   • macOS 15.0.1 24A348 darwin-arm64
    • Mac Designed for iPad (desktop) • mac-designed-for-ipad • darwin         • macOS 15.0.1 24A348 darwin-arm64
    • Chrome (web)                    • chrome                • web-javascript • Google Chrome 131.0.6778.140
    
[✓] Network resources
    • All expected network resources are available.

• No issues found!

```

</details>