Repository: flutter
Issue: [go_router] Exception thrown "Location cannot be empty" because missing location in the configuration state - optionURLReflectsImperativeAPIs = true
Labels: package,has reproducible steps,P2,p: go_router,team-go_router,triaged-go_router,found in release: 3.19,found in release: 3.22

### What package does this bug report belong to?

go_router

### What target platforms are you seeing this bug on?

Web

### Have you already upgraded your packages?

Yes

### Dependency versions

<details><summary>pubspec.lock</summary>

```lock
  go_router:
    dependency: "direct main"
    description:
      path: ".."
      relative: true
    source: path
    version: "13.2.0"
```

</details>


### Steps to reproduce

1. Run the code sample in a web browser (go_router 13.2.0), note: set GoRouter.optionURLReflectsImperativeAPIs = true
2. Click on the center button to open page "details"
3. Click on the back button browser to go home page
4. Click on the forward button to go the details page again, the error will be thrown


### Expected results

There is no error and you can go to the details screen normally

### Actual results

Got the error and can't go to the details page

### Code sample

<details open><summary>Code sample</summary>

```dart
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';

void main() {
  GoRouter.optionURLReflectsImperativeAPIs = true;
  runApp(App());
}

/// The main app.
class App extends StatelessWidget {
  /// Creates an [App].
  App({super.key});

  /// The title of the app.
  static const String title = 'GoRouter Example: Router neglect';

  @override
  Widget build(BuildContext context) => MaterialApp.router(
        routerConfig: _router,
        title: title,
      );

  final GoRouter _router = GoRouter(
    routes: <GoRoute>[
      GoRoute(
          name: "/",
          path: '/',
          builder: (BuildContext context, GoRouterState state) =>
              const Page1Screen(),
          routes: [
            GoRoute(
              name: '/page3',
              path: 'page3',
              builder: (BuildContext context, GoRouterState state) =>
                  const Page3Screen(),
            ),
          ]),
      GoRoute(
        name: '/page2',
        path: '/page2',
        builder: (BuildContext context, GoRouterState state) =>
            const Page2Screen(),
      ),
    ],
  );
}

/// The screen of the first page.
class Page1Screen extends StatelessWidget {
  /// Creates a [Page1Screen].
  const Page1Screen({super.key});

  @override
  Widget build(BuildContext context) => Scaffold(
        appBar: AppBar(title: const Text(App.title)),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              ElevatedButton(
                onPressed: () {
                  Navigator.of(context).popUntil((route) => route.isFirst);
                  context.pushReplacementNamed('/page3');
                },
                child: const Text('Push page 3'),
              ),
            ],
          ),
        ),
      );
}

/// The screen of the second page.
class Page2Screen extends StatelessWidget {
  /// Creates a [Page2Screen].
  const Page2Screen({super.key});

  @override
  Widget build(BuildContext context) => Scaffold(
        appBar: AppBar(title: const Text(App.title)),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              ElevatedButton(
                onPressed: () {
                  Navigator.of(context).popUntil((route) => route.isFirst);
                  context.pushReplacement('/');
                },
                child: const Text('Go to home page'),
              ),
            ],
          ),
        ),
      );
}

/// The screen of the second page.
class Page3Screen extends StatelessWidget {
  /// Creates a [Page3Screen].
  const Page3Screen({super.key});

  @override
  Widget build(BuildContext context) => Scaffold(
        appBar: AppBar(title: Text("page 3")),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              ElevatedButton(
                onPressed: () {
                  GoRouter.of(context).pushReplacementNamed('/');
                },
                child: const Text('Go to home page'),
              ),
            ],
          ),
        ),
      );
}

```

</details>


### Screenshots or Videos

<details open>
<summary>Screenshots / Video demonstration</summary>

[Screencast from 23-04-2024 17:26:37.webm](https://github.com/flutter/flutter/assets/55455388/2bdac6e9-fa61-44e3-ae02-edbd90e19466)


</details>


### Logs

<details open><summary>Logs</summary>

```console
json state: {location: , state: {codec: json, encoded: null}, imperativeMatches: [{location: /page3, state: {codec: json, encoded: null}, pageKey: rn[xl\]piqg_heb]mfikxbfhnb\kvoYe}]}
json state: {location: , state: {codec: json, encoded: null}, imperativeMatches: [{location: /page3, state: {codec: json, encoded: null}, pageKey: rn[xl\]piqg_heb]mfikxbfhnb\kvoYe}]}
json state: {location: /, state: {codec: json, encoded: null}, imperativeMatches: []}
json state: {location: /, state: {codec: json, encoded: null}, imperativeMatches: []}
══╡ EXCEPTION CAUGHT BY FOUNDATION LIBRARY ╞════════════════════════════════════════════════════════
The following GoException was thrown while dispatching notifications for GoRouteInformationProvider:
Location cannot be empty.

When the exception was thrown, this was the stack:
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart 294:49  throw_
packages/go_router/src/path_utils.dart 125:5                                  canonicalUri
packages/go_router/src/configuration.dart 296:31                              findMatch
packages/go_router/src/match.dart 947:23                                      convert
dart-sdk/lib/convert/codec.dart 30:34                                         decode
packages/go_router/src/parser.dart 71:32                                      parseRouteInformationWithDependencies
packages/flutter/src/widgets/router.dart 743:12                               [_processRouteInformation]
packages/flutter/src/widgets/router.dart 762:5                                [_handleRouteInformationProviderNotification]
packages/flutter/src/foundation/change_notifier.dart 403:24                   notifyListeners
packages/go_router/src/information_provider.dart 134:11                       notifyListeners
packages/go_router/src/information_provider.dart 235:5                        [_platformReportsNewRouteInformation]
packages/go_router/src/information_provider.dart 279:5                        didPushRouteInformation
packages/flutter/src/widgets/binding.dart 704:25                              _handlePushRouteInformation
dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 45:50            <fn>
dart-sdk/lib/async/zone.dart 1661:54                                          runUnary
dart-sdk/lib/async/future_impl.dart 156:18                                    handleValue
dart-sdk/lib/async/future_impl.dart 840:44                                    handleValueCallback
dart-sdk/lib/async/future_impl.dart 869:13                                    _propagateToListeners
dart-sdk/lib/async/future_impl.dart 641:5                                     [_completeWithValue]
dart-sdk/lib/async/future_impl.dart 715:7                                     callback
dart-sdk/lib/async/schedule_microtask.dart 40:11                              _microtaskLoop
dart-sdk/lib/async/schedule_microtask.dart 49:5                               _startMicrotaskLoop
dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 181:15           <fn>

The GoRouteInformationProvider sending notification was:
  Instance of 'GoRouteInformationProvider'
════════════════════════════════════════════════════════════════════════════════════════════════════

```

</details>


### Flutter Doctor output

<details open><summary>Doctor output</summary>

```console
[✓] Flutter (Channel stable, 3.13.9, on Ubuntu 22.04.3 LTS 6.5.0-28-generic, locale en_US.UTF-8)
    • Flutter version 3.13.9 on channel stable at /home/hieuho/development/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision d211f42860 (6 months ago), 2023-10-25 13:42:25 -0700
    • Engine revision 0545f8705d
    • Dart version 3.1.5
    • DevTools version 2.25.0

[!] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at /home/hieuho/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/linux#android-setup for more details.

[✓] Chrome - develop for the web
    • Chrome at google-chrome

[✗] Linux toolchain - develop for Linux desktop
    ✗ clang++ is required for Linux development.
      It is likely available from your distribution (e.g.: apt install clang), or can be downloaded from https://releases.llvm.org/
    ✗ CMake is required for Linux development.
      It is likely available from your distribution (e.g.: apt install cmake), or can be downloaded from https://cmake.org/download/
    ✗ ninja is required for Linux development.
      It is likely available from your distribution (e.g.: apt install ninja-build), or can be downloaded from https://github.com/ninja-build/ninja/releases
    • pkg-config version 0.29.2
    ✗ GTK 3.0 development libraries are required for Linux development.
      They are likely available from your distribution (e.g.: apt install libgtk-3-dev)

[✓] Android Studio (version 2023.1)
    • Android Studio at /home/hieuho/development/android-studio
    • 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.7+0-17.0.7b1000.6-10550314)

[✓] VS Code (version 1.87.2)
    • VS Code at /usr/share/code
    • Flutter extension version 3.86.0

[✓] Connected device (2 available)
    • Linux (desktop) • linux  • linux-x64      • Ubuntu 22.04.3 LTS 6.5.0-28-generic
    • Chrome (web)    • chrome • web-javascript • Google Chrome 122.0.6261.111

[✓] Network resources
    • All expected network resources are available.

! Doctor found issues in 2 categories.

```

</details>