Repository: flutter
Issue: restorablePush then pushAndRemoveUntil results in error
Labels: c: crash,platform-android,framework,f: routes,has reproducible steps,P2,a: state restoration,found in release: 3.3,found in release: 3.4,team-android,triaged-android

## Steps to Reproduce

1. Put developer mode on the phone/simulator to 'Don't keep activities' in order to test out restorable navigation.
2. Execute `flutter run` on the code sample
3. Tap on 'Go to Screen 2'
4. Tap on 'Go to Screen 3'
5. Tap on 'Go back to Screen 1'
6. Exit app 
7. Reopen app

**Expected results:**
Opens at Screen 1

**Actual results:**
Get this error:

_AssertionError ('package:flutter/src/widgets/navigator.dart': Failed assertion: line 3310 pos 7: '_history.isNotEmpty': All routes returned by onGenerateInitialRoutes are not restorable. Please make sure that all routes returned by onGenerateInitialRoutes have their RouteSettings defined with names that are defined in the app's routes table.)

Note: I originally had the code without any names for the routes but put them in after seeing this error message (as can be seen below) but obviously that did not solve the issue.

<details>
  <summary>Code sample</summary>

```
import 'package:flutter/material.dart';

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      initialRoute: '/',
      routes: {
        '/': (context) => const Screen1(),
        'screen2': (context) => const Screen2(),
        'screen3': (context) => const Screen3(),
      },
      restorationScopeId: 'root',
    );
  }
}

class Screen1 extends StatefulWidget {
  const Screen1({Key? key}) : super(key: key);

  @override
  State<Screen1> createState() => _Screen1State();
}

class _Screen1State extends State<Screen1> {
  static Route<void> _myRouteBuilder(BuildContext context, Object? arguments) {
    return MaterialPageRoute<void>(
      settings: const RouteSettings(name: '/screen2'),
      builder: (BuildContext context) => const Screen2(),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: TextButton(
            onPressed: () {
              Navigator.restorablePush(context, _myRouteBuilder);
            },
            child: const Text('Go to Screen 2')),
      ),
    );
  }
}

class Screen2 extends StatefulWidget {
  const Screen2({Key? key}) : super(key: key);

  @override
  State<Screen2> createState() => _Screen2State();
}

class _Screen2State extends State<Screen2> {
  static Route<void> _myRouteBuilder(BuildContext context, Object? arguments) {
    return MaterialPageRoute<void>(
      settings: const RouteSettings(name: '/screen3'),
      builder: (BuildContext context) => const Screen3(),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(title: const Text('Screen 2')),
        body: Center(
            child: TextButton(
          onPressed: () => Navigator.restorablePush(context, _myRouteBuilder),
          child: const Text('Go to Screen 3'),
        )));
  }
}

class Screen3 extends StatelessWidget {
  const Screen3({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(title: const Text('Screen 3')),
        body: Center(
          child: TextButton(
            onPressed: () => Navigator.pushAndRemoveUntil(
                context,
                MaterialPageRoute(builder: (context) => const Screen1()),
                ((route) => false)),
            child: const Text('Go back to Screen 1'),
          ),
        ));
  }
}

```


</details>

<details>
  <summary>Logs</summary>

```
══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY
╞═══════════════════════════════════════════════════════════
                    The following assertion was thrown building IconTheme(color: Color(0xdd000000)):
                    All routes returned by onGenerateInitialRoutes are not restorable. Please make
sure that all routes
                    returned by onGenerateInitialRoutes have their RouteSettings defined with names
that are defined in
                    the app's routes table.
                    'package:flutter/src/widgets/navigator.dart':
                    Failed assertion: line 3310 pos 7: '_history.isNotEmpty'
                    
                    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.md
                    
                    The relevant error-causing widget was:
                      MaterialApp
MaterialApp:file:///<removed some personal deets>/flutter_sandbox/lib/main.dart:10:12
                    
                    When the exception was thrown, this was the stack:
                    #2      NavigatorState.restoreState
(package:flutter/src/widgets/navigator.dart:3310:7)
                    #3      RestorationMixin._doRestore
(package:flutter/src/widgets/restoration.dart:887:5)
                    #4      RestorationMixin.didChangeDependencies
(package:flutter/src/widgets/restoration.dart:873:7)
                    #5      NavigatorState.didChangeDependencies
(package:flutter/src/widgets/navigator.dart:3336:11)
                    #6      StatefulElement._firstBuild
(package:flutter/src/widgets/framework.dart:4963:11)
                    #7      ComponentElement.mount
(package:flutter/src/widgets/framework.dart:4781:5)
                    ...     Normal element mounting (267 frames)
                    #274    Element.inflateWidget
(package:flutter/src/widgets/framework.dart:3817:16)
                    #275    Element.updateChild (package:flutter/src/widgets/framework.dart:3545:20)
                    #276    ComponentElement.performRebuild
(package:flutter/src/widgets/framework.dart:4832:16)
                    #277    StatefulElement.performRebuild
(package:flutter/src/widgets/framework.dart:4977:11)
                    #278    Element.rebuild (package:flutter/src/widgets/framework.dart:4529:5)
                    #279    BuildOwner.buildScope
(package:flutter/src/widgets/framework.dart:2659:19)
                    #280    WidgetsBinding.drawFrame
(package:flutter/src/widgets/binding.dart:891:21)
                    #281    RendererBinding._handlePersistentFrameCallback
                    (package:flutter/src/rendering/binding.dart:370:5)
                    #282    SchedulerBinding._invokeFrameCallback
(package:flutter/src/scheduler/binding.dart:1146:15)
                    #283    SchedulerBinding.handleDrawFrame
(package:flutter/src/scheduler/binding.dart:1083:9)
                    #284    SchedulerBinding.scheduleWarmUpFrame.<anonymous closure>
                    (package:flutter/src/scheduler/binding.dart:864:7)
                    (elided 13 frames from class _AssertionError, class _RawReceivePortImpl, class
_Timer, dart:async,
                    and dart:async-patch)
```


`flutter analyze` output:
```
No issues found! (ran in 5.4s)
```

`flutter doctor -v` output:

```
[✓] Flutter (Channel stable, 3.0.2, on macOS 12.2.1 21D62 darwin-x64, locale en-AU)
    • Flutter version 3.0.2 at /Users/<personal info>/flutter_test/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision cd41fdd495 (4 months ago), 2022-06-08 09:52:13 -0700
    • Engine revision f15f824b57
    • Dart version 2.17.3
    • DevTools version 2.12.2

Checking Android licenses is taking an unexpectedly long time...[✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
    • Android SDK at /Users/<personal info>/Library/Android/sdk
    • Platform android-33, build-tools 31.0.0
    • Java binary at: /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_252-b09)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 13.3.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.71.0)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.48.0

[✓] Connected device (3 available)
    • Redmi Note 8 Pro (mobile) • m7jno7iv4dgu6x8x • android-arm64  • Android 11 (API 30)
    • macOS (desktop)           • macos            • darwin-x64     • macOS 12.2.1 21D62 darwin-x64
    • Chrome (web)              • chrome           • web-javascript • Google Chrome 105.0.5195.125

[✓] HTTP Host Availability
    • All required HTTP hosts are available
```
Note: I'm running my app on the Redmi Note 8 Pro

</details>