[ios][pv]fix platform view crash due to screenScale=0 (#165525)

This is a regression introduced by:
https://github.com/flutter/flutter/pull/162785/files#diff-b08b98ecaa5bd434f8b4181793f2dad9389c32c5b8cdedb52b01a2d3355f51a9R26

It's reported by one of the customers. We are not able to reproduce it. 

It's likely that `flutterView` itself is `nil` (rather than
`flutterView.screen` being `nil`), because we reset `flutterView` when
backgrounding the app

*List which issues are fixed by this PR. You must list at least one
issue. An issue is not required if the PR fixes something trivial like a
typo.*

b/403598670

*If you had to change anything in the [flutter/tests] repo, include a
link to the migration guide as per the [breaking change policy].*

## Pre-launch Checklist

- [x] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [x] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [x] I read and followed the [Flutter Style Guide], including [Features
we expect every widget to implement].
- [x] I signed the [CLA].
- [x] I listed at least one issue that this PR fixes in the description
above.
- [x] I updated/added relevant documentation (doc comments with `///`).
- [x] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [x] I followed the [breaking change policy] and added [Data Driven
Fixes] where supported.
- [x] All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel
on [Discord].

<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview
[Tree Hygiene]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md
[test-exempt]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests
[Flutter Style Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md
[Features we expect every widget to implement]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement
[CLA]: https://cla.developers.google.com/
[flutter/tests]: https://github.com/flutter/tests
[breaking change policy]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes
[Discord]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md
[Data Driven Fixes]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformViewsTest.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformViewsTest.mm
index 7b8d2a4..8d2269c 100644
--- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformViewsTest.mm
+++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformViewsTest.mm
@@ -4624,9 +4624,9 @@
   FlutterEngine* engine = [[FlutterEngine alloc] initWithName:@"foobar"];
   [engine run];
   XCTAssertTrue(engine.platformView != nullptr);
-  auto ios_context = engine.platformView->GetIosContext();
+  std::shared_ptr<flutter::IOSContext> ios_context = engine.platformView->GetIosContext();
 
-  auto pool = flutter::OverlayLayerPool{};
+  flutter::OverlayLayerPool pool;
 
   // Add layers to the pool.
   pool.CreateLayer(ios_context, MTLPixelFormatBGRA8Unorm, 1);
@@ -4644,6 +4644,26 @@
   XCTAssertEqual(pool.size(), 1u);
 }
 
+- (void)testLayerUpdateViewStateWithNilFlutterViewShouldNotCrash {
+  // Create an IOSContext.
+  FlutterEngine* engine = [[FlutterEngine alloc] initWithName:@"foobar"];
+  [engine run];
+  XCTAssertTrue(engine.platformView != nullptr);
+  std::shared_ptr<flutter::IOSContext> ios_context = engine.platformView->GetIosContext();
+
+  flutter::OverlayLayerPool pool;
+
+  // Add layers to the pool.
+  pool.CreateLayer(ios_context, MTLPixelFormatBGRA8Unorm, 1);
+  XCTAssertEqual(pool.size(), 1u);
+
+  std::shared_ptr<flutter::OverlayLayer> layer = pool.GetNextLayer();
+
+  layer->UpdateViewState(nil, SkRect::MakeXYWH(1, 2, 3, 4), 0, 0);
+  // Should not update the view state (e.g. overlay_view_wrapper's frame) when FlutterView is nil.
+  XCTAssertTrue(CGRectEqualToRect(layer->overlay_view_wrapper.frame, CGRectZero));
+}
+
 - (void)testFlutterPlatformViewControllerSubmitFramePreservingFrameDamage {
   flutter::FlutterPlatformViewsTestMockPlatformViewDelegate mock_delegate;
 
diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/overlay_layer_pool.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/overlay_layer_pool.mm
index 89b61ca..8cd0e4f 100644
--- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/overlay_layer_pool.mm
+++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/overlay_layer_pool.mm
@@ -23,6 +23,11 @@
                                    SkRect rect,
                                    int64_t view_id,
                                    int64_t overlay_id) {
+  // There can be a race where UpdateViewState() is called when flutter_view is nil when app is
+  // backgrounded.
+  if (!flutter_view) {
+    return;
+  }
   auto screenScale = ((FlutterView*)flutter_view).screen.scale;
   // Set the size of the overlay view wrapper.
   // This wrapper view masks the overlay view.