Repository: flutter
Issue: REOPEN | Flutter does not get the correct initial route when launched as cold/killed app
Labels: waiting for customer response,in triage

### Steps to reproduce

1. Integrate deep link via: https://docs.flutter.dev/development/ui/navigation/deep-linking 
2. Run the app in release mode in emulator to see logs
3. Kill the app (Remove from background)
4. Click the link
5. Following logs are being printed out

```bash
I/flutter ( 3947): ---------------result initial: splash
I/flutter ( 3947): _platformCallHandler call onAnonymousIdChanged {payload: {anonymousUserID: 00000194-87cf-a307-bc11-865d8b473b98}, platform: android}
I/flutter ( 4232): ---------------result initial: https://sastaticket.app/link/flights/flight-search?origin=ISB&destination=RUH&departure=2025-01-25
I/flutter ( 4232): Null check operator used on a null value
```
And since the deep link isn't a route, it navigates to `"/"` route which crashes the app.

Related Closed Issue: https://github.com/flutter/flutter/issues/121244

### Expected results

1. App should lead to specific screen handled via deep link

### Actual results

Case # 1:
1. App is crashing when opened as cold app i.e. after killing it
NOTE: It works fine when the app is in the background

Case # 2:
1. If we put `'/': (context) => HomeScreen(),` just to cover the default route, it still throws the exception but the deep link works

Screen recording:

https://github.com/user-attachments/assets/df886d3f-c56b-4e91-a75c-c2397db9a4c7

Logs:





### Code sample

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

```dart
class AppDeepLink {
  static late GlobalKey<NavigatorState> _navigator;
  static StreamSubscription? _sub;

  static init(GlobalKey<NavigatorState> state) async {
    _navigator = state;
    _sub = AppLinks().uriLinkStream.listen((uri) {
      parseUrl(uri.toString());
    });
  }

  static parseUrl(String? raw) {
    if (raw == null) return;
    /// Handles the URI parsing
 }

/// rest of the code
}
```

</details>


### Screenshots or Video

<details open>
<summary>Screenshots / Video demonstration</summary>

### Not Working Grey Screen (App is killed)
https://github.com/user-attachments/assets/dc79489b-091d-4fa5-9ab9-e93ff462e451

### Working Screen (App is in background)

https://github.com/user-attachments/assets/8a778f4e-cb91-4ad8-843f-7fe4a57ab883

</details>


### Logs

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

```console
I/flutter ( 8283): ---------------result initial: https://sastaticket.app/link/flights/flight-search?origin=ISB&destination=RUH&departure=2025-01-25
I/flutter ( 8283): Null check operator used on a null value
I/flutter ( 8283): #0      _WidgetsAppState._onUnknownRoute (package:flutter/src/widgets/app.dart:1577)
I/flutter ( 8283): #1      NavigatorState._routeNamed (package:flutter/src/widgets/navigator.dart:4572)
I/flutter ( 8283): #2      Navigator.defaultGenerateInitialRoutes (package:flutter/src/widgets/navigator.dart:2937)
I/flutter ( 8283): #3      NavigatorState.restoreState (package:flutter/src/widgets/navigator.dart:3778)
I/flutter ( 8283): #4      RestorationMixin._doRestore (package:flutter/src/widgets/restoration.dart:928)
I/flutter ( 8283): #5      RestorationMixin.didChangeDependencies (package:flutter/src/widgets/restoration.dart:914)
I/flutter ( 8283): #6      NavigatorState.didChangeDependencies (package:flutter/src/widgets/navigator.dart:3825)
I/flutter ( 8283): #7      StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:5780)
I/flutter ( 8283): #8      ComponentElement.mount (package:flutter/src/widgets/framework.dart:5607)
I/flutter ( 8283): #9      Element.inflateWidget (package:flutter/src/widgets/framework.dart:4480)
I/flutter ( 8283): #10     Element.updateChild (package:flutter/src/widgets/framework.dart:3963)
I/flutter ( 8283): #11     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5656)
I/flutter ( 8283): #12     Element.rebuild (package:flutter/src/widgets/framework.dart:5347)
I/flutter ( 8283): #13     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:5613)
I/flutter ( 8283): #14     ComponentElement.mount (package:flutter/src/widgets/framework.dart:5607)
I/flutter ( 8283): #15     Element.inflateWidget (package:flutter/src/widgets/framework.dart:4480)
I/flutter ( 8283): #16     Element.updateChild (package:flutter/src/widgets/framework.dart:3963)
I/flutter ( 8283): #17     SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6914)
I/flutter ( 8283): #18     Element.inflateWidget (package:flutter/src/widgets/framework.dart:4480)
I/flutter ( 8283): #19     Element.updateChild (package:flutter/src/widgets/framework.dart:3963)
I/flutter ( 8283): #20     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5656)
I/flutter ( 8283): #21     StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:5794)
I/flutter ( 8283): #22     Element.rebuild (package:flutter/src/widgets/framework.dart:5347)
I/flutter ( 8283): #23     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:5613)
I/flutter ( 8283): #24     StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:5785)
I/flutter ( 8283): #25     ComponentElement.mount (package:flutter/src/widgets/framework.dart:5607)
I/flutter ( 8283): #26     Element.inflateWidget (package:flutter/src/widgets/framework.dart:4480)
I/flutter ( 8283): #27     Element.updateChild (package:flutter/src/widgets/framework.dart:3963)
I/flutter ( 8283): #28     SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6914)
I/flutter ( 8283): #29     Element.inflateWidget (package:flutter/src/widgets/framework.dart:4480)
I/flutter ( 8283): #30     Element.updateChild (package:flutter/src/widgets/framework.dart:3963)
I/flutter ( 8283): #31     SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6914)
I/flutter ( 8283): #32     Element.inflateWidget (package:flutter/src/widgets/framework.dart:4480)
I/flutter ( 8283): #33     Element.updateChild (package:flutter/src/widgets/framework.dart:3963)
I/flutter ( 8283): #34     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5656)
I/flutter ( 8283): #35     StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:5794)
I/flutter ( 8283): #36     Element.rebuild (package:flutter/src/widgets/framework.dart:5347)
I/flutter ( 8283): #37     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:5613)
I/flutter ( 8283): #38     StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:5785)
I/flutter ( 8283): #39     ComponentElement.mount (package:flutter/src/widgets/framework.dart:5607)
I/flutter ( 8283): #40     Element.inflateWidget (package:flutter/src/widgets/framework.dart:4480)
I/flutter ( 8283): #41     MultiChildRenderObjectElement.inflateWidget (package:flutter/src/widgets/framework.dart:7049)
I/flutter ( 8283): #42     MultiChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:7061)
I/flutter ( 8283): #43     Element.inflateWidget (package:flutter/src/widgets/framework.dart:4480)
I/flutter ( 8283): #44     Element.updateChild (package:flutter/src/widgets/framework.dart:3963)
I/flutter ( 8283): #45     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5656)
I/flutter ( 8283): #46     Element.rebuild (package:flutter/src/widgets/framework.dart:5347)
I/flutter ( 8283): #47     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:5613)
I/flutter ( 8283): #48     ComponentElement.mount (package:flutter/src/widgets/framework.dart:5607)
I/flutter ( 8283): #49     Element.inflateWidget (package:flutter/src/widgets/framework.dart:4480)
I/flutter ( 8283): #50     Element.updateChild (package:flutter/src/widgets/framework.dart:3963)
I/flutter ( 8283): #51     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5656)
I/flutter ( 8283): #52     Element.rebuild (package:flutter/src/widgets/framework.dart:5347)
I/flutter ( 8283): #53     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:5613)
I/flutter ( 8283): #54     ComponentElement.mount (package:flutter/src/widgets/framework.dart:5607)
I/flutter ( 8283): #55     Element.inflateWidget (package:flutter/src/widgets/framework.dart:4480)
I/flutter ( 8283): #56     Element.updateChild (package:flutter/src/widgets/framework.dart:3963)
I/flutter ( 8283): #57     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5656)
I/flutter ( 8283): #58     Element.rebuild (package:flutter/src/widgets/framework.dart:5347)
I/flutter ( 8283): #59     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:5613)
I/flutter ( 8283): #60     ComponentElement.mount (package:flutter/src/widgets/framework.dart:5607)
I/flutter ( 8283): #61     Element.inflateWidget (package:flutter/src/widgets/framework.dart:4480)
I/flutter ( 8283): #62     Element.updateChild (package:flutter/src/widgets/framework.dart:3963)
I/flutter ( 8283): #63     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5656)
I/flutter ( 8283): #64     Element.rebuild (package:flutter/src/widgets/framework.dart:5347)
I/flutter ( 8283): #65     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:5613)
I/flutter ( 8283): #66     ComponentElement.mount (package:flutter/src/widgets/framework.dart:5607)
I/flutter ( 8283): #67     Element.inflateWidget (package:flutter/src/widgets/framework.dart:4480)
I/flutter ( 8283): #68     MultiChildRenderObjectElement.inflateWidget (package:flutter/src/widgets/framework.dart:7049)
I/flutter ( 8283): #69     MultiChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:7061)
I/flutter ( 8283): #70     Element.inflateWidget (package:flutter/src/widgets/framework.dart:4480)
I/flutter ( 8283): #71     Element.updateChild (package:flutter/src/widgets/framework.dart:3963)
I/flutter ( 8283): #72     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5656)
I/flutter ( 8283): #73     Element.rebuild (package:flutter/src/widgets/framework.dart:5347)
I/flutter ( 8283): #74     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:5613)
I/flutter ( 8283): #75     ComponentElement.mount (package:flutter/src/widgets/framework.dart:5607)
I/flutter ( 8283): #76     Element.inflateWidget (package:flutter/src/widgets/framework.dart:4480)
I/flutter ( 8283): #77     Element.updateChild (package:flutter/src/widgets/framework.dart:3963)
I/flutter ( 8283): #78     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5656)
I/flutter ( 8283): #79     StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:5794)
I/flutter ( 8283): #80     Element.rebuild (package:flutter/src/widgets/framework.dart:5347)
I/flutter ( 8283): #81     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:5613)
I/flutter ( 8283): #82     StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:5785)
I/flutter ( 8283): #83     ComponentElement.mount (package:flutter/src/widgets/framework.dart:5607)
I/flutter ( 8283): #84     Element.inflateWidget (package:flutter/src/widgets/framework.dart:4480)
I/flutter ( 8283): #85     Element.updateChild (package:flutter/src/widgets/framework.dart:3963)
I/flutter ( 8283): #86     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5656)
I/flutter ( 8283): #87     StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:5794)
I/flutter ( 8283): #88     Element.rebuild (package:flutter/src/widgets/framework.dart:5347)
I/flutter ( 8283): #89     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:5613)
I/flutter ( 8283): #90     StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:5785)
I/flutter ( 8283): #91     ComponentElement.mount (package:flutter/src/widgets/framework.dart:5607)
I/flutter ( 8283): #92     Element.inflateWidget (package:flutter/src/widgets/framework.dart:4480)
I/flutter ( 8283): #93     Element.updateChild (package:flutter/src/widgets/framework.dart:3963)
I/flutter ( 8283): #94     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5656)
I/flutter ( 8283): #95     Element.rebuild (package:flutter/src/widgets/framework.dart:5347)
I/flutter ( 8283): #96     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:5613)
I/flutter ( 8283): #97     ComponentElement.mount (package:flutter/src/widgets/framework.dart:5607)
I/flutter ( 8283): #98     Element.inflateWidget (package:flutter/src/widgets/framework.dart:4480)
I/flutter ( 8283): #99     Element.updateChild (package:flutter/src/widgets/framework.dart:3963)
I/flutter ( 8283): ---------------map: {type: link, product: flights, action: flight-search, origin: ISB, destination: RUH, departure: 2025-01-25}
I/flutter ( 8283): _platformCallHandler call onAnonymousIdChanged {payload: {anonymousUserID: 00000194-87cf-a307-bc11-865d8b473b98}, platform: android}
I/flutter ( 8283): Another exception was thrown: Instance of 'DiagnosticsProperty<void>'
I/flutter ( 8556): ---------------result initial: splash
I/flutter ( 8556): _platformCallHandler call onAnonymousIdChanged {payload: {anonymousUserID: 00000194-87cf-a307-bc11-865d8b473b98}, platform: android}
I/flutter ( 8556): ---------------map: {type: link, product: flights, action: flight-search, origin: ISB, destination: RUH, departure: 2025-01-25}
```

</details>


### Flutter Doctor output

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

```console
[✓] Flutter (Channel stable, 3.27.0, on macOS 15.2 24C101 darwin-arm64, locale en-PK)
    • Flutter version 3.27.0 on channel stable at /Users/hamza/Development/flutter327
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 8495dee1fd (6 weeks ago), 2024-12-10 14:23:39 -0800
    • Engine revision 83bacfc525
    • Dart version 3.6.0
    • DevTools version 2.40.2

[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at /Users/hamza/Library/Android/sdk
    • Platform android-35, build-tools 34.0.0
    • ANDROID_HOME = /Users/hamza/Library/Android/sdk
    • ANDROID_SDK_ROOT = /Users/hamza/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-11609105)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 16.2)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 16C5032a
    • CocoaPods version 1.16.2

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2024.1)
    • 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-11609105)

[✓] VS Code (version 1.96.3)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.102.0

[✓] Connected device (4 available)
    • sdk gphone64 arm64 (mobile)     • emulator-5554         • android-arm64  • Android 12 (API 31) (emulator)
    • macOS (desktop)                 • macos                 • darwin-arm64   • macOS 15.2 24C101 darwin-arm64
    • Mac Designed for iPad (desktop) • mac-designed-for-ipad • darwin         • macOS 15.2 24C101 darwin-arm64
    • Chrome (web)                    • chrome                • web-javascript • Google Chrome 131.0.6778.265

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

• No issues found!
```

</details>