Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Do not use try/catch for injectPlatformView flow.
PR comments.
  • Loading branch information
ditman committed Dec 20, 2023
commit deba54dad2a83a469b447232a5f5c11f553202ba
11 changes: 7 additions & 4 deletions lib/web_ui/lib/src/engine/platform_views/content_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,18 @@ class PlatformViewManager {
return _contents.containsKey(viewId);
}

/// Returns the cached contents of [viewId], to inject them into the DOM.
/// Returns the cached contents of [viewId], to be injected into the DOM.
///
/// This is only used by the active `Renderer` object when a platform view needs
/// to be injected in the DOM, through `FlutterView.DomManager.injectPlatformView`.
///
/// This may return null, if [renderContent] was not called before this. The
/// framework seems to allow/need this for some tests, so it is allowed here
/// as well.
///
/// App programmers should not access this directly, and instead use [getViewById].
DomElement getSlottedContent(int viewId) {
assert(knowsViewId(viewId), 'No platform view has been rendered with id: $viewId');
return _contents[viewId]!;
DomElement? getSlottedContent(int viewId) {
return _contents[viewId];
}

/// Returns the HTML element created by a registered factory for [viewId].
Expand Down
19 changes: 9 additions & 10 deletions lib/web_ui/lib/src/engine/view_embedder/dom_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -220,17 +220,16 @@ class DomManager {
void injectPlatformView(int platformViewId) {
// For now, we don't need anything fancier. If needed, this can be converted
// to a PlatformViewStrategy class for each web-renderer backend?
try {
final DomElement pv = PlatformViewManager.instance.getSlottedContent(platformViewId);
// If pv is already a descendant of platformViewsHost -> noop
if (pv.parent == platformViewsHost) {
return;
}
platformViewsHost.append(pv);
} catch (e) {
// Do not fail, many tests expect this to not crash!
domWindow.console.warn(e);
final DomElement? pv = PlatformViewManager.instance.getSlottedContent(platformViewId);
if (pv == null) {
domWindow.console.debug('Failed to inject Platform View Id: $platformViewId. Call render first!');
return;
}
// If pv is already a descendant of platformViewsHost -> noop
if (pv.parent == platformViewsHost) {
return;
}
platformViewsHost.append(pv);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,15 +240,17 @@ void testMain() {
expect(view2.isDisposed, isFalse);

bool onMetricsChangedCalled = false;
dispatcher.onMetricsChanged = () {
dispatcher.onMetricsChanged = () {
onMetricsChangedCalled = true;
};

expect(onMetricsChangedCalled, isFalse);

dispatcher.viewManager.disposeAndUnregisterView(view2.viewId);

expect(onMetricsChangedCalled, isTrue);
expect(onMetricsChangedCalled, isTrue, reason: 'onMetricsChanged should have been called.');

dispatcher.dispose();
});
});
}
Expand Down