Repository: flutter
Issue: [go_router] TypedGoRoute seems to be incompatible with PopScope
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

go_router: ^14.0.2
go_router_builder: ^2.6.2

### Steps to reproduce

1. Click FloatingActionButton to go to the second page.
2. Click appbar back button.

Related issue： #119677 #137545 #138737

### Expected results

Back to the first page without exception.

### Actual results

Throw “Null check operator used on a null value”.

### Code sample

<details open><summary>Code sample</summary>

```dart
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';

part 'main.g.dart';

void main() => runApp(MaterialApp.router(routerConfig: appRouter));

final appRouter = GoRouter(
  routes: $appRoutes,
);

@TypedGoRoute<FirstRoute>(
  path: '/',
  routes: [
    TypedGoRoute<SecondRoute>(
      path: 'second',
    ),
  ],
)
class FirstRoute extends GoRouteData {
  @override
  Widget build(BuildContext context, GoRouterState state) => const FirstPage();
}

class SecondRoute extends GoRouteData {
  @override
  Widget build(BuildContext context, GoRouterState state) => const SecondPage();
}

class FirstPage extends StatelessWidget {
  const FirstPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      floatingActionButton: FloatingActionButton(onPressed: () => SecondRoute().go(context)),
    );
  }
}

class SecondPage extends StatelessWidget {
  const SecondPage({super.key});

  @override
  Widget build(BuildContext context) {
    return PopScope(
      canPop: false,
      onPopInvoked: (_) {
        context.pop();
      },
      child: Scaffold(
        appBar: AppBar(),
      ),
    );
  }
}

```

</details>


### Screenshots or Video

<details open>
<summary>Screenshots / Video demonstration</summary>

![截圖 2024-05-08 上午11 15 53](https://github.com/flutter/flutter/assets/25738593/cf01d6df-72bb-4202-bbec-239b7d022a40)

</details>


### Logs

<details open><summary>Logs</summary>

```console

======== Exception caught by widgets library =======================================================
The following _TypeError was thrown building HeroControllerScope:
Null check operator used on a null value

The relevant error-causing widget was: 
  MaterialApp MaterialApp:file:///Users/chien/Flutter/untitled4/lib/main.dart:6:35
When the exception was thrown, this was the stack: 
#0      _CustomNavigatorState._handlePopPage (package:go_router/src/builder.dart:415:61)
#1      NavigatorState.pop (package:flutter/src/widgets/navigator.dart:5284:28)
#2      GoRouterDelegate.pop (package:go_router/src/delegate.dart:115:11)
#3      GoRouter.pop (package:go_router/src/router.dart:490:20)
#4      GoRouterHelper.pop (package:go_router/src/misc/extensions.dart:71:65)
#5      SecondPage.build.<anonymous closure> (package:untitled4/main.dart:50:17)
#6      ModalRoute.onPopInvoked (package:flutter/src/widgets/routes.dart:1578:30)
#7      _RouteEntry.pop (package:flutter/src/widgets/navigator.dart:3116:11)
#8      _RouteEntry.markForPop (package:flutter/src/widgets/navigator.dart:3322:5)
#9      DefaultTransitionDelegate.resolve.handleExitingRoute (package:flutter/src/widgets/navigator.dart:1105:28)
#10     DefaultTransitionDelegate.resolve (package:flutter/src/widgets/navigator.dart:1144:7)
#11     TransitionDelegate._transition (package:flutter/src/widgets/navigator.dart:921:53)
#12     NavigatorState._updatePages (package:flutter/src/widgets/navigator.dart:4181:43)
#13     NavigatorState.didUpdateWidget (package:flutter/src/widgets/navigator.dart:3841:7)
#14     StatefulElement.update (package:flutter/src/widgets/framework.dart:5652:55)
#15     Element.updateChild (package:flutter/src/widgets/framework.dart:3824:15)
#16     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5505:16)
#17     Element.rebuild (package:flutter/src/widgets/framework.dart:5196:7)
#18     ProxyElement.update (package:flutter/src/widgets/framework.dart:5809:5)
#19     Element.updateChild (package:flutter/src/widgets/framework.dart:3824:15)
#20     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5505:16)
#21     Element.rebuild (package:flutter/src/widgets/framework.dart:5196:7)
#22     ProxyElement.update (package:flutter/src/widgets/framework.dart:5809:5)
#23     _InheritedNotifierElement.update (package:flutter/src/widgets/inherited_notifier.dart:105:11)
#24     Element.updateChild (package:flutter/src/widgets/framework.dart:3824:15)
#25     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5505:16)
#26     StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:5643:11)
#27     Element.rebuild (package:flutter/src/widgets/framework.dart:5196:7)
#28     StatefulElement.update (package:flutter/src/widgets/framework.dart:5666:5)
#29     Element.updateChild (package:flutter/src/widgets/framework.dart:3824:15)
#30     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5505:16)
#31     Element.rebuild (package:flutter/src/widgets/framework.dart:5196:7)
#32     ProxyElement.update (package:flutter/src/widgets/framework.dart:5809:5)
#33     Element.updateChild (package:flutter/src/widgets/framework.dart:3824:15)
#34     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5505:16)
#35     Element.rebuild (package:flutter/src/widgets/framework.dart:5196:7)
#36     StatelessElement.update (package:flutter/src/widgets/framework.dart:5556:5)
#37     Element.updateChild (package:flutter/src/widgets/framework.dart:3824:15)
#38     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5505:16)
#39     Element.rebuild (package:flutter/src/widgets/framework.dart:5196:7)
#40     ProxyElement.update (package:flutter/src/widgets/framework.dart:5809:5)
#41     Element.updateChild (package:flutter/src/widgets/framework.dart:3824:15)
#42     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5505:16)
#43     Element.rebuild (package:flutter/src/widgets/framework.dart:5196:7)
#44     ProxyElement.update (package:flutter/src/widgets/framework.dart:5809:5)
#45     Element.updateChild (package:flutter/src/widgets/framework.dart:3824:15)
#46     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5505:16)
#47     StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:5643:11)
#48     Element.rebuild (package:flutter/src/widgets/framework.dart:5196:7)
#49     BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2904:19)
#50     WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:989:21)
#51     RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:448:5)
#52     SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1386:15)
#53     SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1311:9)
#54     SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:1169:5)
#55     _invoke (dart:ui/hooks.dart:312:13)
#56     PlatformDispatcher._drawFrame (dart:ui/platform_dispatcher.dart:399:5)
#57     _drawFrame (dart:ui/hooks.dart:283:31)
====================================================================================================

======== Exception caught by widgets library =======================================================
The following assertion was thrown building Navigator-[LabeledGlobalKey<NavigatorState>#61df0](dirty, dependencies: [HeroControllerScope, UnmanagedRestorationScope], state: NavigatorState#f66f1(tickers: tracking 2 tickers)):
'package:flutter/src/widgets/navigator.dart': Failed assertion: line 5534 pos 12: '!_debugLocked': is not true.


Either the assertion indicates an error in the framework itself, or we should provide substantially more information in this error message to help you determine and fix the underlying cause.
In either case, please report this assertion by filing a bug on GitHub:
  https://github.com/flutter/flutter/issues/new?template=2_bug.yml

The relevant error-causing widget was: 
  MaterialApp MaterialApp:file:///Users/chien/Flutter/untitled4/lib/main.dart:6:35
When the exception was thrown, this was the stack: 
#2      NavigatorState.build (package:flutter/src/widgets/navigator.dart:5534:12)
#3      StatefulElement.build (package:flutter/src/widgets/framework.dart:5592:27)
#4      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5480:15)
#5      StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:5643:11)
#6      Element.rebuild (package:flutter/src/widgets/framework.dart:5196:7)
#7      BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2904:19)
#8      WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:989:21)
#9      RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:448:5)
#10     SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1386:15)
#11     SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1311:9)
#12     SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:1169:5)
#13     _invoke (dart:ui/hooks.dart:312:13)
#14     PlatformDispatcher._drawFrame (dart:ui/platform_dispatcher.dart:399:5)
#15     _drawFrame (dart:ui/hooks.dart:283:31)
(elided 2 frames from class _AssertionError)
====================================================================================================

======== Exception caught by widgets library =======================================================
The following assertion was thrown building Navigator-[LabeledGlobalKey<NavigatorState>#61df0](dependencies: [HeroControllerScope, UnmanagedRestorationScope], state: NavigatorState#f66f1(tickers: tracking 2 tickers)):
'package:flutter/src/widgets/framework.dart': Failed assertion: line 6786 pos 12: 'renderObject.child == child': is not true.


Either the assertion indicates an error in the framework itself, or we should provide substantially more information in this error message to help you determine and fix the underlying cause.
In either case, please report this assertion by filing a bug on GitHub:
  https://github.com/flutter/flutter/issues/new?template=2_bug.yml

The relevant error-causing widget was: 
  MaterialApp MaterialApp:file:///Users/chien/Flutter/untitled4/lib/main.dart:6:35
When the exception was thrown, this was the stack: 
#2      SingleChildRenderObjectElement.removeRenderObjectChild (package:flutter/src/widgets/framework.dart:6786:12)
#3      RenderObjectElement.detachRenderObject (package:flutter/src/widgets/framework.dart:6601:37)
#4      Element.detachRenderObject.<anonymous closure> (package:flutter/src/widgets/framework.dart:4202:13)
#5      ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:5532:14)
#6      Element.detachRenderObject (package:flutter/src/widgets/framework.dart:4201:5)
#7      Element.detachRenderObject.<anonymous closure> (package:flutter/src/widgets/framework.dart:4202:13)
#8      ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:5532:14)
#9      Element.detachRenderObject (package:flutter/src/widgets/framework.dart:4201:5)
#10     Element.deactivateChild (package:flutter/src/widgets/framework.dart:4376:11)
#11     Element.updateChild (package:flutter/src/widgets/framework.dart:3835:9)
#12     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5505:16)
#13     StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:5643:11)
#14     Element.rebuild (package:flutter/src/widgets/framework.dart:5196:7)
#15     BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2904:19)
#16     WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:989:21)
#17     RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:448:5)
#18     SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1386:15)
#19     SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1311:9)
#20     SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:1169:5)
#21     _invoke (dart:ui/hooks.dart:312:13)
#22     PlatformDispatcher._drawFrame (dart:ui/platform_dispatcher.dart:399:5)
#23     _drawFrame (dart:ui/hooks.dart:283:31)
(elided 2 frames from class _AssertionError)
====================================================================================================

======== Exception caught by scheduler library =====================================================
The following assertion was thrown during a scheduler callback:
Tried to build dirty widget in the wrong build scope.

A widget which was marked as dirty and is still active was scheduled to be built, but the current build scope unexpectedly does not contain that widget.

Sometimes this is detected when an element is removed from the widget tree, but the element somehow did not get marked as inactive. In that case, it might be caused by an ancestor element failing to implement visitChildren correctly, thus preventing some or all of its descendants from being correctly deactivated.

The root of the build scope was: [root]
The offending element (which does not appear to be a descendant of the root of the build scope) was: _IconButtonM3
  style: ButtonStyle#5600b(mouseCursor: _IconButtonDefaultMouseCursor#4636e)
  dirty
  dependencies: [IconButtonTheme, IconTheme, _InheritedTheme, _LocalizationsScope-[GlobalKey#28302]]
  state: _ButtonStyleState#2e9e8
When the exception was thrown, this was the stack: 
#0      BuildOwner.buildScope.<anonymous closure> (package:flutter/src/widgets/framework.dart:2863:13)
#1      BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2888:10)
#2      WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:989:21)
#3      RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:448:5)
#4      SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1386:15)
#5      SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1311:9)
#6      SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:1169:5)
#7      _invoke (dart:ui/hooks.dart:312:13)
#8      PlatformDispatcher._drawFrame (dart:ui/platform_dispatcher.dart:399:5)
#9      _drawFrame (dart:ui/hooks.dart:283:31)
====================================================================================================

```

</details>


### Flutter Doctor output

<details open><summary>Doctor output</summary>

```console
[✓] Flutter (Channel stable, 3.19.6, on macOS 14.4.1 23E224 darwin-arm64, locale zh-Hant-TW)
    • Flutter version 3.19.6 on channel stable at /Library/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 54e66469a9 (3 weeks ago), 2024-04-17 13:08:03 -0700
    • Engine revision c4cd48e186
    • Dart version 3.3.4
    • DevTools version 2.31.1

[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at /Users/chien/Library/Android/sdk
    • Platform android-34, build-tools 34.0.0
    • ANDROID_SDK_ROOT = /Users/chien/Library/Android/sdk
    • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.10+0-17.0.10b1087.21-11572160)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 15.3)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 15E204a
    • CocoaPods version 1.15.0

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2023.3)
    • 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 17.0.10+0-17.0.10b1087.21-11572160)

[✓] IntelliJ IDEA Community Edition (version 2024.1.1)
    • IntelliJ at /Applications/IntelliJ IDEA CE.app
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin version 241.15989.9

[✓] VS Code (version 1.89.0)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.88.0

[✓] Connected device (4 available)
    • Android SDK built for arm64 (mobile) • emulator-5554                        • android-arm64  • Android 9 (API 28)
      (emulator)
    • iPhone SE (3rd generation) (mobile)  • 0F2A5E6F-C1A4-4496-942F-E4AFE7E3067C • ios            •
      com.apple.CoreSimulator.SimRuntime.iOS-17-0 (simulator)
    • macOS (desktop)                      • macos                                • darwin-arm64   • macOS 14.4.1 23E224
      darwin-arm64
    • Chrome (web)                         • chrome                               • web-javascript • Google Chrome
      124.0.6367.119
    ! Error: Browsing on the local area network for Cybertan iPhone. Ensure the device is unlocked and attached with a cable
      or associated with the same local area network as this Mac.
      The device must be opted into Developer Mode to connect wirelessly. (code -27)

[✓] Network resources
    • All expected network resources are available.

```

</details>