Repository: flutter
Issue: [go_router] Pop immediately after replace doesn't work
Labels: package,has reproducible steps,P2,p: go_router,team-go_router,triaged-go_router,found in release: 3.24,found in release: 3.27

### Steps to reproduce

1. Use go_router: 14.6.2
2. Create an app with 3 routes A, B, and C
3. Use A as the initial route
4. Push route B
5. In a button callback, replace the current route with C, then immediately pop the current route
6. See that the app stays on route C instead of going back to A

### Expected results

The app should be on route A

### Actual results

The app is on route C

### Code sample

<details open><summary>Code sample</summary>

```dart
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';

void main() async {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp.router(
      routerConfig: GoRouter(
        initialLocation: '/a',
        routes: <RouteBase>[
          GoRoute(
            path: '/a',
            builder: (_, __) {
              return const ScreenA();
            },
          ),
          GoRoute(
            path: '/b',
            builder: (_, __) {
              return const ScreenB();
            },
          ),
          GoRoute(
            path: '/c',
            builder: (_, __) {
              return const ScreenC();
            },
          ),
        ],
      ),
    );
  }
}

class ScreenA extends StatelessWidget {
  const ScreenA({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: [
          const Text(
            'Screen A',
            textAlign: TextAlign.center,
          ),
          ElevatedButton(
            onPressed: () {
              GoRouter.of(context).push('/b');
            },
            child: const Text('Push screen B'),
          ),
        ],
      ),
    );
  }
}

class ScreenB extends StatelessWidget {
  const ScreenB({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: [
          const Text(
            'Screen B',
            textAlign: TextAlign.center,
          ),
          ElevatedButton(
            onPressed: () {
              GoRouter.of(context).replace('/c');
              GoRouter.of(context).pop();
            },
            child: const Text('Replace then pop'),
          ),
        ],
      ),
    );
  }
}

class ScreenC extends StatelessWidget {
  const ScreenC({super.key});

  @override
  Widget build(BuildContext context) {
    return const Scaffold(
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: [
          Text(
            'Screen C',
            textAlign: TextAlign.center,
          ),
        ],
      ),
    );
  }
}

```


### Flutter Doctor output

<details open><summary>Doctor output</summary>

```console
[!] Flutter (Channel [user-branch], 3.24.1, on Microsoft Windows [Version 10.0.19045.5131], locale en-CA)
    ! Flutter version 3.24.1 on channel [user-branch] at F:\Coding\Languages\flutter
      Currently on an unknown channel. Run `flutter channel` to switch to an official channel.
      If that doesn't fix the issue, reinstall Flutter by following instructions at https://flutter.dev/setup.
    ! Upstream repository unknown source is not a standard remote.
      Set environment variable "FLUTTER_GIT_URL" to unknown source to dismiss this error.
    • Framework revision 5874a72aa4 (4 months ago), 2024-08-20 16:46:00 -0500
    • Engine revision c9b9d5780d
    • Dart version 3.5.1
    • DevTools version 2.37.2
    • If those were intentional, you can disregard the above warnings; however it is recommended to use "git" directly to perform update checks and upgrades.

[√] Windows Version (Installed version of Windows is version 10 or higher)       
                                                                                 
[√] Android toolchain - develop for Android devices (Android SDK version 34.0.0) 
    • Android SDK at C:\Coding\IDEs\Android\AndroidSDK                           
    • Platform android-34, build-tools 34.0.0                                    
    • ANDROID_HOME = C:\Coding\IDEs\Android\AndroidSDK                           
    • Java binary at: C:\Coding\IDEs\Android\Android Studio\jbr\bin\java         
    • Java version OpenJDK Runtime Environment (build 17.0.9+0--11185874)        
    • All Android licenses accepted.                                             
                                                                                 
[√] Chrome - develop for the web                                                 
    • Chrome at C:\Program Files (x86)\Google\Chrome\Application\chrome.exe      
                                                                                 
[√] Visual Studio - develop Windows apps (Visual Studio Professional 2022 17.9.2)
    • Visual Studio at C:\Program Files\Microsoft Visual Studio\2022\Professional
    • Visual Studio Professional 2022 version 17.9.34622.214
    • Windows 10 SDK version 10.0.22000.0

[√] Android Studio (version 2023.2)
    • Android Studio at C:\Coding\IDEs\Android\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
    • android-studio-dir = C:\Coding\IDEs\Android\Android Studio
    • Java version OpenJDK Runtime Environment (build 17.0.9+0--11185874)

[√] IntelliJ IDEA Community Edition (version 2021.2)
    • IntelliJ at F:\Coding\IDEs\IntelliJ IDEA Community Edition 2021.2.2
    • 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

[√] Connected device (4 available)
    • SM G990W2 (mobile) • R5CTC13W9MM • android-arm64  • Android 14 (API 34)
    • Windows (desktop)  • windows     • windows-x64    • Microsoft Windows [Version 10.0.19045.5131]
    • Chrome (web)       • chrome      • web-javascript • Google Chrome 131.0.6778.70
    • Edge (web)         • edge        • web-javascript • Microsoft Edge 131.0.2903.86

[√] Network resources
    • All expected network resources are available.

! Doctor found issues in 1 category.

```

</details>