Repository: flutter
Issue:  [go_router] pop ignores redirect
Labels: package,has reproducible steps,P2,p: go_router,found in release: 3.0,found in release: 3.1,team-go_router,triaged-go_router

### to reproduce
 
 1. enable the switch
 2. navigate to `bar/baz`
 3. deactivate the switch
 4. tap the back button
 
 ### expected result
 
 should redirect to `foo`
 
 ### actual result
 
 navigates to `bar`

<details>
  <summary>code sample</summary>

```dart
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';

void main() => runApp(const AllowedWidget(child: MyApp()));

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  late final GoRouter _router;

  @override
  void dispose() {
    _router.dispose();
    super.dispose();
  }

  @override
  Widget build(context) {
    final allow = InheritedAllowedWidget.of(context).notifier;
    _router = GoRouter(
      routes: <GoRoute>[
        GoRoute(path: '/', redirect: (state) => '/foo'),
        GoRoute(
          path: '/foo',
          builder: (context, state) => MyPage(
            key: state.pageKey,
            nexts: const ['/bar', '/bar/baz'],
          ),
        ),
        GoRoute(
          path: '/bar',
          builder: (context, state) => MyPage(
            key: state.pageKey,
            nexts: const ['/bar/baz'],
          ),
          redirect: (state) => allow.value ? null : '/foo',
          routes: [
            GoRoute(
              path: 'baz',
              builder: (context, state) => MyPage(
                key: state.pageKey,
                nexts: const ['/foo'],
              ),
            ),
          ],
        ),
      ],
    );
    return MaterialApp.router(
      routeInformationParser: _router.routeInformationParser,
      routerDelegate: _router.routerDelegate,
      routeInformationProvider: _router.routeInformationProvider,
    );
  }
}

class MyPage extends StatelessWidget {
  final Iterable<String> nexts;
  const MyPage({
    required this.nexts,
    Key? key,
  }) : super(key: key);

  @override
  Widget build(context) {
    final allow = InheritedAllowedWidget.of(context).notifier;
    return Scaffold(
      appBar: AppBar(
        title: Text(GoRouter.of(context).location),
      ),
      body: Column(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        children: [
          for (final next in nexts)
            Center(
              child: TextButton(
                child: Text(next),
                onPressed: () => context.go(next),
              ),
            ),
          Center(
            child: ValueListenableBuilder<bool>(
              valueListenable: allow,
              builder: (context, allowed, _) => Switch(
                value: allowed,
                onChanged: (value) => allow.value = value,
              ),
            ),
          ),
        ],
      ),
    );
  }
}

class InheritedAllowedWidget extends InheritedWidget {
  const InheritedAllowedWidget({
    Key? key,
    required this.notifier,
    required Widget child,
  }) : super(
          key: key,
          child: child,
        );

  final ValueNotifier<bool> notifier;

  static InheritedAllowedWidget of(BuildContext context) {
    final InheritedAllowedWidget? result =
        context.dependOnInheritedWidgetOfExactType<InheritedAllowedWidget>();
    assert(result != null, 'No InheritedAllowedWidget found in context');
    return result!;
  }

  @override
  bool updateShouldNotify(InheritedAllowedWidget oldWidget) =>
      notifier.hashCode != oldWidget.notifier.hashCode;
}

class AllowedWidget extends StatefulWidget {
  final Widget child;
  const AllowedWidget({
    required this.child,
    Key? key,
  }) : super(key: key);

  @override
  State<AllowedWidget> createState() => _AllowedWidgetState();
}

class _AllowedWidgetState extends State<AllowedWidget> {
  late final ValueNotifier<bool> _notifier;

  @override
  void initState() {
    _notifier = ValueNotifier(false);
    super.initState();
  }

  @override
  void dispose() {
    _notifier.dispose();
    super.dispose();
  }

  @override
  Widget build(context) => InheritedAllowedWidget(
        notifier: _notifier,
        child: widget.child,
      );
}

```

</details>

<details>
  <summary>pubspec</summary>

```yaml
name: issues_101183
description: A new Flutter project.
version: 1.0.0+1
publish_to: 'none'

environment:
  sdk: '>=2.17.6 <3.0.0'

dependencies:
  flutter:
    sdk: flutter
  go_router:
    git:
     url: https://github.com/flutter/packages.git
     path: packages/go_router
     ref: go_router_v5

dev_dependencies:
  flutter_lints: ^2.0.1
  
flutter:
  uses-material-design: true
```

</details>


</details>

<details>
  <summary>doctor</summary>

```console
[✓] Flutter (Channel stable, 3.0.5, on macOS 12.5 21G72 darwin-arm, locale en-EE)
    • Flutter version 3.0.5 at /Users/yakforward/fvm/versions/stable
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision f1875d570e (6 weeks ago), 2022-07-13 11:24:16 -0700
    • Engine revision e85ea0e79c
    • Dart version 2.17.6
    • DevTools version 2.12.2

[✓] Android toolchain - develop for Android devices (Android SDK version 32.1.0-rc1)
    • Android SDK at /Users/yakforward/Library/Android/sdk
    • Platform android-32, build-tools 32.1.0-rc1
    • Java binary at: /Library/Java/JavaVirtualMachines/temurin-18.jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment Temurin-18.0.2+9 (build 18.0.2+9)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 13.4.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • CocoaPods version 1.11.3

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[!] Android Studio (not installed)
    • Android Studio not found; download from https://developer.android.com/studio/index.html
      (or visit https://flutter.dev/docs/get-started/install/macos#android-setup for detailed instructions).

[✓] VS Code (version 1.70.2)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.46.0

[✓] Connected device (2 available)
    • macOS (desktop) • macos  • darwin-arm64   • macOS 12.5 21G72 darwin-arm
    • Chrome (web)    • chrome • web-javascript • Google Chrome 104.0.5112.101

[✓] HTTP Host Availability
    • All required HTTP hosts are available

! Doctor found issues in 1 category.
```

</details>

see also 
- https://github.com/flutter/flutter/issues/103437 
- https://github.com/flutter/flutter/issues/104907 
- https://github.com/flutter/flutter/issues/107712 
- https://github.com/flutter/flutter/issues/145173

_Originally posted by @iapicca in https://github.com/flutter/flutter/issues/101183#issuecomment-1207728406_