Fix `SemanticsObject` crash when bridge is nullptr (#185293)

This should fix #174853 (I have not verified myself but the bridge
availability check should prevent the potential null dereference).


## Pre-launch Checklist

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

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

If this change needs to override an active code freeze, provide a
comment explaining why. The code freeze workflow can be overridden by
code reviewers. See pinned issues for any active code freezes with
guidance.

**Note**: The Flutter team is currently trialing the use of [Gemini Code
Assist for
GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code).
Comments from the `gemini-code-assist` bot should not be taken as
authoritative feedback from the Flutter team. If you find its comments
useful you can update your code accordingly, but if you are unsure or
disagree with the feedback, please feel free to wait for a Flutter team
member's review for guidance on which automated comments should be
addressed.

<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview
[AI contribution guidelines]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#ai-contribution-guidelines
[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/SemanticsObject+UIFocusSystem.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/SemanticsObject+UIFocusSystem.mm
index c9b64f5..c21012f 100644
--- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/SemanticsObject+UIFocusSystem.mm
+++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/SemanticsObject+UIFocusSystem.mm
@@ -61,7 +61,7 @@
 
 - (id<UIFocusEnvironment>)parentFocusEnvironment {
   // The root SemanticsObject node's parent is the FlutterView.
-  return self.parent.focusItem ?: self.bridge->view();
+  return self.parent.focusItem ?: ([self isAccessibilityBridgeAlive] ? self.bridge->view() : nil);
 }
 
 - (NSArray<id<UIFocusEnvironment>>*)preferredFocusEnvironments {
@@ -89,6 +89,9 @@
 // See also the `coordinateSpace` implementation.
 // TODO(LongCatIsLooong): use CoreGraphics types.
 - (CGRect)frame {
+  if (![self isAccessibilityBridgeAlive]) {
+    return CGRectZero;
+  }
   SkPoint quad[4] = {SkPoint::Make(self.node.rect.left(), self.node.rect.top()),
                      SkPoint::Make(self.node.rect.left(), self.node.rect.bottom()),
                      SkPoint::Make(self.node.rect.right(), self.node.rect.top()),
@@ -159,7 +162,8 @@
 
 - (id<UICoordinateSpace>)coordinateSpace {
   // A regular SemanticsObject uses the same coordinate space as its parent.
-  return self.parent.coordinateSpace ?: self.bridge->view();
+  return self.parent.coordinateSpace
+             ?: ([self isAccessibilityBridgeAlive] ? self.bridge->view() : nil);
 }
 
 @end
diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/SemanticsObjectTest.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/SemanticsObjectTest.mm
index 6750878..fee32ce 100644
--- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/SemanticsObjectTest.mm
+++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/SemanticsObjectTest.mm
@@ -41,6 +41,22 @@
   XCTAssertNotNil(object);
 }
 
+- (void)testUIFocusSystemMethodsDoNotCrashWhenBridgeIsDead {
+  fml::WeakPtr<flutter::AccessibilityBridgeIos> bridge;
+  SemanticsObject* object;
+  {
+    auto mock_bridge = std::make_unique<flutter::testing::MockAccessibilityBridge>();
+    fml::WeakPtrFactory<flutter::AccessibilityBridgeIos> factory(mock_bridge.get());
+    bridge = factory.GetWeakPtr();
+    object = [[SemanticsObject alloc] initWithBridge:bridge uid:0];
+  }
+  // Now bridge is nullptr.
+
+  XCTAssertNil([object parentFocusEnvironment]);
+  XCTAssertNil([object coordinateSpace]);
+  XCTAssertTrue(CGRectEqualToRect([object frame], CGRectZero));
+}
+
 - (void)testSetChildren {
   fml::WeakPtrFactory<flutter::AccessibilityBridgeIos> factory(
       new flutter::testing::MockAccessibilityBridge());