Repository: flutter
Issue: _debugRelayoutBoundaryAlreadyMarkedNeedsLayout() is not true with StatefulShellRoute
Labels: package,a: error message,has reproducible steps,p: go_router,team-framework,found in release: 3.27,found in release: 3.28

### Steps to reproduce

1. Launch the code sample in debug mode.
2. Tap "Go to Screen A" right away. 
   There is a 3 seconds timer in the background, the button has to be tapped during this time frame.
3. The timer triggers rebuild of ScreenB while it exists in the background StatefulShellBranch.
4. ScreenB gets a new child widget after the build.

### Expected results

The app keeps running normally.

### Actual results

The app crashes.

### Code sample

Flutter 3.27.2
go_router 14.6.3

<details open><summary>Code sample</summary>

```dart
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp.router(
      routerConfig: GoRouter(
        initialLocation: '/b/2',
        routes: [
          StatefulShellRoute.indexedStack(
            branches: [
              StatefulShellBranch(routes: [
                GoRoute(
                  path: '/a',
                  builder: (context, state) => const ScreenA(),
                ),
              ]),
              StatefulShellBranch(routes: [
                GoRoute(
                  path: '/b',
                  builder: (context, state) => const ScreenB(),
                  routes: [
                    GoRoute(
                      path: '2',
                      builder: (context, state) => const ScreenB2(),
                    ),
                  ],
                ),
              ]),
            ],
            builder: (context, state, navigationShell) => navigationShell,
          ),
        ],
      ),
    );
  }
}

class ScreenA extends StatelessWidget {
  const ScreenA({super.key});

  @override
  Widget build(BuildContext context) {
    return const Scaffold(body: Text("Screen A"));
  }
}

class ScreenB extends StatefulWidget {
  const ScreenB({super.key});

  @override
  State<ScreenB> createState() => _ScreenBState();
}

class _ScreenBState extends State<ScreenB> {
  Timer? _timer;
  bool _showNewChild = false;

  @override
  void initState() {
    super.initState();
    // Use a timer to trigger a rebuild.
    _timer = Timer(const Duration(seconds: 3), () {
      print("Trigger");
      setState(() {
        _showNewChild = true;
      });
    });
  }

  @override
  void dispose() {
    _timer?.cancel();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: [
          const Text("Screen B"),
          // Appearance of this widget triggers the crash.
          if (_showNewChild) const Text("New Child"),
        ],
      ),
    );
  }
}

class ScreenB2 extends StatelessWidget {
  const ScreenB2({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: [
          const Text("Screen B/2"),
          FilledButton(
            onPressed: () {
              context.go("/a");
            },
            child: const Text("Go to Screen A"),
          ),
        ],
      ),
    );
  }
}

```

</details>


### Screenshots or Video




### Logs

<details open><summary>Logs</summary>

```console
Trigger
══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
The following assertion was thrown building Text("New Child", dependencies: [DefaultSelectionStyle,
DefaultTextStyle, MediaQuery]):
Assertion failed:
file:///home/oleg/flutter/flutter/packages/flutter/lib/src/rendering/object.dart:2329:14
_debugRelayoutBoundaryAlreadyMarkedNeedsLayout()
is not true

The relevant error-causing widget was:
  Text Text:file:///home/oleg/Projects/test_flutter_crash/lib/main.dart:92:36

When the exception was thrown, this was the stack:
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart 288:3     throw_
dart-sdk/lib/_internal/js_dev_runtime/private/profile.dart 110:39               assertFailed
packages/flutter/src/rendering/object.dart 2329:14                              markNeedsLayout
packages/flutter/src/rendering/box.dart 2669:11                                 markNeedsLayout
packages/flutter/src/rendering/object.dart 1855:5                               adoptChild
packages/flutter/src/rendering/object.dart 4355:5                               insert
packages/flutter/src/widgets/framework.dart 6988:17                             insertRenderObjectChild
packages/flutter/src/widgets/framework.dart 6746:35                             attachRenderObject
packages/flutter/src/widgets/framework.dart 6611:5                              mount
packages/flutter/src/widgets/framework.dart 7056:11                             mount
packages/flutter/src/widgets/framework.dart 4480:15                             inflateWidget
packages/flutter/src/widgets/framework.dart 3963:18                             updateChild
packages/flutter/src/widgets/framework.dart 5656:16                             performRebuild
packages/flutter/src/widgets/framework.dart 5347:7                              rebuild
packages/flutter/src/widgets/framework.dart 5613:5                              [_firstBuild]
packages/flutter/src/widgets/framework.dart 5607:5                              mount
packages/flutter/src/widgets/framework.dart 4480:15                             inflateWidget
packages/flutter/src/widgets/framework.dart 7049:36                             inflateWidget
packages/flutter/src/widgets/framework.dart 3963:18                             updateChild
packages/flutter/src/widgets/framework.dart 4150:32                             updateChildren
packages/flutter/src/widgets/framework.dart 7074:17                             update
packages/flutter/src/widgets/framework.dart 3941:14                             updateChild
packages/flutter/src/widgets/framework.dart 5656:16                             performRebuild
packages/flutter/src/widgets/framework.dart 5347:7                              rebuild
packages/flutter/src/widgets/framework.dart 5707:5                              update
<...>
```

</details>


### Flutter Doctor output

<details open><summary>Doctor output</summary>

```console
[✓] Flutter (Channel stable, 3.27.2, on Ubuntu 22.04.2 LTS 5.19.0-41-generic, locale ru_RU.UTF-8)
    • Flutter version 3.27.2 on channel stable at /home/oleg/flutter/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 68415ad1d9 (3 дня назад), 2025-01-13 10:22:03 -0800
    • Engine revision e672b006cb
    • Dart version 3.6.1
    • DevTools version 2.40.2

[✓] Android toolchain - develop for Android devices (Android SDK version 35.0.0)
    • Android SDK at /home/oleg/Android/Sdk
    • Platform android-35, build-tools 35.0.0
    • Java binary at: /usr/lib/jvm/java-18-openjdk-amd64/bin/java
    • Java version OpenJDK Runtime Environment (build 18.0.2-ea+9-Ubuntu-222.04)
    • All Android licenses accepted.

[✓] Chrome - develop for the web
    • Chrome at google-chrome

[✗] Linux toolchain - develop for Linux desktop
    • Ubuntu clang version 14.0.0-1ubuntu1.1
    • cmake version 3.22.1
    • ninja version 1.10.1
    • 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 2024.2)
    • Android Studio at /home/oleg/Apps/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 21.0.3+-12282718-b509.11)

[✓] VS Code (version 1.93.1)
    • VS Code at /usr/share/code
    • Flutter extension version 3.102.0

[✓] Connected device (2 available)
    • Linux (desktop)    • linux    • linux-x64      • Ubuntu 22.04.2 LTS 5.19.0-41-generic
    • Chrome (web)       • chrome   • web-javascript • Google Chrome 129.0.6668.70

[✓] Network resources
    • All expected network resources are available.

! Doctor found issues in 1 category.
```

</details>