Repository: flutter
Issue: Getting _InternalLinkedHashMap cast error only when context has been restored
Labels: framework,has reproducible steps,P2,a: state restoration,found in release: 2.10,found in release: 2.13,team-framework,triaged-framework

 I'm trying to allow my pages to be restorable, even though some arguments are passed to those pages.

## Steps to Reproduce

1. Execute code sample
2. Navigate normally and tap in button with the corresponding action
3. Put app in background (with Don't Keep Activities enabled in Developer Options) and bring it back

**Expected results:**

By what is shown on the sample code I'd expect the code to run in the same way from Navigation and from Restorations. Which doesn't seem to happen, even though the debugger data shows up as being exactly the same.

**Actual results:**

When I run the sample code for the first time (not from restoration, but by navigation) I get NO errors and my page loads normally.
When I put the app in the background, as mentioned above, and bring it back (forcing a restoration) I get a "_InternalLinkedHashMap cast" error. According to the debugger data there should be no problems when casting those arguments.

<details>
<summary>Code sample and Error images</summary>

<!--
      Please create a minimal reproducible sample that shows the problem
      and attach it below between the lines with the backticks.

      To create it you can use `flutter create bug` command and update the `main.dart` file.

      Alternatively, you can use https://dartpad.dev/
      which is capable of creating and running small Flutter apps.

      Without this we will unlikely be able to progress on the issue, and because of that
      we regretfully will have to close it.
-->

```dart
child: MaterialApp(
  restorationScopeId: 'root_app',
  onGenerateRoute: Routes.buildRoutes(),
  title: 'My App',
  theme: ThemeData(primarySwatch: Colors.teal),
)
```

```dart
static RouteFactory buildRoutes() {
  return (settings) {
    final arguments = settings.arguments != null ? settings.arguments as Map<String, dynamic> : <String, dynamic>{};
    Widget _view;

    var route = findRoute(settings.name);
    switch (route) {
      case Routes.menu:
        _view = const Menu();
        break;
      case Routes.selection:
        _view = const Page2(arguments);
        break;
      default:
        return null;
    }

    return MaterialPageRoute(
        settings: settings,
        builder: (context) => RestorationScope(restorationId: route.restorationId, child: _view),
    );
  };
}
```

```dart
onSuccess: (List<Map<String, dynamic>> evalData) =>
  Navigator.restorablePushReplacementNamed(context, widget._route.value, arguments: <String, dynamic>{'data': evalData}),
```
</details>

<details>
  <summary>Logs</summary>

From my actual code:
```
PS E:\Projetos\Flutter\ludo_simulado_2022> flutter run -t lib/app_main.dart
Using hardware rendering with device Android SDK built for x86. If you notice graphics artifacts, consider enabling software rendering with "--enable-software-rendering".
Launching lib/app_main.dart on Android SDK built for x86 in debug mode...
Running Gradle task 'assembleDebug'...                             18,1s
√  Built build\app\outputs\flutter-apk\app-debug.apk.
D/EGL_emulation(23111): eglMakeCurrent: 0xa38c3620: ver 3 0 (tinfo 0x8f58e400)
D/EGL_emulation(23111): eglMakeCurrent: 0xa38c3620: ver 3 0 (tinfo 0x8f58e400)
Syncing files to device Android SDK built for x86...               240ms

Flutter run key commands.
r Hot reload. 
R Hot restart.
h List all available interactive commands.
d Detach (terminate "flutter run" but leave application running).
c Clear the screen
q Quit (terminate the application on the device).

 Running with sound null safety 

An Observatory debugger and profiler on Android SDK built for x86 is available at: http://127.0.0.1:54106/gOsOG6QJJLs=/
I/zygote  (23111): Do partial code cache collection, code=14KB, data=20KB
I/zygote  (23111): After code cache collection, code=14KB, data=20KB
I/zygote  (23111): Increasing code cache capacity to 128KB
D/skia    (23111): Shader compilation error
D/skia    (23111): ------------------------
D/skia    (23111): Errors:
D/skia    (23111): link failed but did not provide an info log
D/skia    (23111): Shader compilation error
D/skia    (23111): ------------------------
D/skia    (23111): Errors:
D/skia    (23111): link failed but did not provide an info log
D/skia    (23111): Shader compilation error
D/skia    (23111): ------------------------
D/skia    (23111): Errors:
D/skia    (23111): link failed but did not provide an info log
D/skia    (23111): Shader compilation error
D/skia    (23111): ------------------------
D/skia    (23111): Errors:
D/skia    (23111): link failed but did not provide an info log
D/skia    (23111): Shader compilation error
D/skia    (23111): ------------------------
D/skia    (23111): Errors:
D/skia    (23111): link failed but did not provide an info log
D/EGL_emulation(23111): eglMakeCurrent: 0xa38c3500: ver 3 0 (tinfo 0x8cd0f5f0)
The Flutter DevTools debugger and profiler on Android SDK built for x86 is available at: http://127.0.0.1:9101?uri=http://127.0.0.1:54106/gOsOG6QJJLs=/
D/skia    (23111): Shader compilation error
D/skia    (23111): ------------------------
D/skia    (23111): Errors:
D/skia    (23111): link failed but did not provide an info log
D/skia    (23111): Shader compilation error
D/skia    (23111): ------------------------
D/skia    (23111): Errors:
D/skia    (23111): link failed but did not provide an info log
D/skia    (23111): Shader compilation error
D/skia    (23111): ------------------------
D/skia    (23111): Errors:
D/skia    (23111): link failed but did not provide an info log
D/skia    (23111): Shader compilation error
D/skia    (23111): ------------------------
D/skia    (23111): Errors:
D/skia    (23111): link failed but did not provide an info log
D/skia    (23111): Shader compilation error
D/skia    (23111): ------------------------
D/skia    (23111): Errors:
D/skia    (23111): link failed but did not provide an info log
D/skia    (23111): Shader compilation error
D/skia    (23111): ------------------------
D/skia    (23111): Errors:
D/skia    (23111): link failed but did not provide an info log
D/skia    (23111): Shader compilation error
D/skia    (23111): ------------------------
D/skia    (23111): Errors:
D/skia    (23111): link failed but did not provide an info log
D/skia    (23111): Shader compilation error
D/skia    (23111): ------------------------
D/skia    (23111): Errors:
D/skia    (23111): link failed but did not provide an info log
D/skia    (23111): Shader compilation error
D/skia    (23111): ------------------------
D/skia    (23111): Errors:
D/skia    (23111): link failed but did not provide an info log
D/skia    (23111): Shader compilation error
D/skia    (23111): ------------------------
D/skia    (23111): Errors:
D/skia    (23111): link failed but did not provide an info log
D/skia    (23111): Shader compilation error
D/skia    (23111): ------------------------
D/skia    (23111): Errors:
D/skia    (23111): link failed but did not provide an info log
D/skia    (23111): Shader compilation error
D/skia    (23111): ------------------------
D/skia    (23111): Errors:
D/skia    (23111): link failed but did not provide an info log
D/skia    (23111): Shader compilation error
D/skia    (23111): ------------------------
D/skia    (23111): Errors:
D/skia    (23111): link failed but did not provide an info log
D/EGL_emulation(23111): eglMakeCurrent: 0xa38c3500: ver 3 0 (tinfo 0x8cd0f5f0)
E/libEGL  (23111): call to OpenGL ES API with no current context (logged once per thread)
D/EGL_emulation(23111): eglMakeCurrent: 0xa38c3620: ver 3 0 (tinfo 0x8f58e400)
D/EGL_emulation(23111): eglCreateContext: 0xacee3080: maj 3 min 0 rcv 3
D/EGL_emulation(23111): eglCreateContext: 0xacee30e0: maj 3 min 0 rcv 3
D/        (23111): HostConnection::get() New Host Connection established 0xa5298480, tid 23163
D/EGL_emulation(23111): eglMakeCurrent: 0xacee30e0: ver 3 0 (tinfo 0xa5283520)
D/PathProviderPlugin(23111): Use TaskQueues.
I/zygote  (23111): Do partial code cache collection, code=61KB, data=54KB
I/zygote  (23111): After code cache collection, code=61KB, data=54KB
I/zygote  (23111): Increasing code cache capacity to 256KB
D/        (23111): HostConnection::get() New Host Connection established 0x8f5a8180, tid 23162
D/EGL_emulation(23111): eglMakeCurrent: 0xacee3080: ver 3 0 (tinfo 0xa4fd8fc0)
D/EGL_emulation(23111): eglCreateContext: 0xacee37a0: maj 3 min 0 rcv 3
D/EGL_emulation(23111): eglMakeCurrent: 0xacee37a0: ver 3 0 (tinfo 0xa4fd8fc0)
D/EGL_emulation(23111): eglMakeCurrent: 0xacee3080: ver 3 0 (tinfo 0xa4fd8fc0)
D/EGL_emulation(23111): eglMakeCurrent: 0xacee3080: ver 3 0 (tinfo 0xa4fd8fc0)
D/EGL_emulation(23111): eglMakeCurrent: 0xacee3080: ver 3 0 (tinfo 0xa4fd8fc0)

══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
The following _CastError was thrown building IconTheme(color: Color(0xdd000000)):
type '_InternalLinkedHashMap<Object?, Object?>' is not a subtype of type 'Map<String, dynamic>' in
type cast

The relevant error-causing widget was:
  MaterialApp MaterialApp:file:///E:/Projetos/Flutter/ludo_simulado_2022/lib/app_main.dart:27:14

When the exception was thrown, this was the stack:
#0      Routes.buildRoutes.<anonymous closure> (package:ludo_simulado_2022/app_routes.dart:35:44)
#1      _WidgetsAppState._onGenerateRoute (package:flutter/src/widgets/app.dart:1370:37)
#2      NavigatorState._routeNamed (package:flutter/src/widgets/navigator.dart:4049:46)
#3      _NamedRestorationInformation.createRoute (package:flutter/src/widgets/navigator.dart:5265:44)
#4      _RestorationInformation.toRouteEntry (package:flutter/src/widgets/navigator.dart:5222:34)
#5      _HistoryProperty.restoreEntriesForPage (package:flutter/src/widgets/navigator.dart:5442:69)
#6      NavigatorState.restoreState (package:flutter/src/widgets/navigator.dart:3267:42)
#7      RestorationMixin._doRestore (package:flutter/src/widgets/restoration.dart:887:5)
#8      RestorationMixin.didChangeDependencies (package:flutter/src/widgets/restoration.dart:873:7)
#9      NavigatorState.didChangeDependencies (package:flutter/src/widgets/navigator.dart:3336:11)
#10     StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4963:11)
#11     ComponentElement.mount (package:flutter/src/widgets/framework.dart:4781:5)
...     Normal element mounting (267 frames)
#278    Element.inflateWidget (package:flutter/src/widgets/framework.dart:3817:16)
#279    Element.updateChild (package:flutter/src/widgets/framework.dart:3545:20)
#280    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4832:16)
#281    StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4977:11)
#282    Element.rebuild (package:flutter/src/widgets/framework.dart:4529:5)
#283    BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2659:19)
#284    WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:891:21)
#285    RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:367:5)
#286    SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1146:15)
#287    SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1083:9)
#288    SchedulerBinding.scheduleWarmUpFrame.<anonymous closure> (package:flutter/src/scheduler/binding.dart:864:7)
(elided 11 frames from class _RawReceivePortImpl, class _Timer, dart:async, and dart:async-patch)

════════════════════════════════════════════════════════════════════════════════════════════════════
D/skia    (23111): Shader compilation error
D/skia    (23111): ------------------------
D/skia    (23111): Errors:
D/skia    (23111): link failed but did not provide an info log
D/skia    (23111): Shader compilation error
D/skia    (23111): ------------------------
D/skia    (23111): Errors:
D/skia    (23111): link failed but did not provide an info log
D/skia    (23111): Shader compilation error
D/skia    (23111): ------------------------
D/skia    (23111): Errors:
D/skia    (23111): link failed but did not provide an info log
D/skia    (23111): Shader compilation error
D/skia    (23111): ------------------------
D/skia    (23111): Errors:
D/skia    (23111): link failed but did not provide an info log
D/EGL_emulation(23111): eglMakeCurrent: 0xa38c3500: ver 3 0 (tinfo 0x8cd0f5f0)
```

```
PS E:\Projetos\Flutter\ludo_simulado_2022> flutter analyze
Analyzing ludo_simulado_2022...                                         
No issues found! (ran in 17.3s)
```

```
PS E:\Projetos\Flutter\ludo_simulado_2022> flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel beta, 2.13.0-0.2.pre, on Microsoft Windows [versÆo 10.0.19044.1645], locale pt-BR)
[√] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
[√] Chrome - develop for the web
[√] Visual Studio - develop for Windows (Visual Studio Community 2022 17.1.3)
[!] Android Studio (not installed)
[√] Connected device (4 available)
[√] HTTP Host Availability

! Doctor found issues in 1 category.
```
</details>

As stated, in both situations the debugger shows me that "settings.arguments" is a Map with an key "data", but during restoration I do get a Cast error:

![Error](https://user-images.githubusercontent.com/293596/165330700-79b5c8b2-938e-4e7a-8433-b808f0fb2237.png)

![Data](https://user-images.githubusercontent.com/293596/165330715-a57dbe00-aac9-4e2e-93f1-f889451cb6b3.png)