From be4b051279153ac8afcf65e43c3cca47691d5828 Mon Sep 17 00:00:00 2001 From: Tanimowo Obaloluwa <113532607+TanimowoObaloluwaDavid@users.noreply.github.com> Date: Sat, 3 Oct 2026 21:41:53 +0100 Subject: [PATCH] fix(share_plus): fail with a PlatformException instead of crashing when there is no implicit view --- .../share_plus/windows/share_plus_plugin.cpp | 23 ++++++++++++++++++- .../windows/share_plus_windows_plugin.h | 4 ++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/packages/share_plus/share_plus/windows/share_plus_plugin.cpp b/packages/share_plus/share_plus/windows/share_plus_plugin.cpp index fed28d46cb..3087954f59 100644 --- a/packages/share_plus/share_plus/windows/share_plus_plugin.cpp +++ b/packages/share_plus/share_plus/windows/share_plus_plugin.cpp @@ -38,7 +38,13 @@ SharePlusWindowsPlugin::~SharePlusWindowsPlugin() { } HWND SharePlusWindowsPlugin::GetWindow() { - return ::GetAncestor(registrar_->GetView()->GetNativeWindow(), GA_ROOT); + // |GetView()| returns null when the engine has no implicit view, so this + // returns null instead of dereferencing it. + auto view = registrar_->GetView(); + if (view == nullptr) { + return nullptr; + } + return ::GetAncestor(view->GetNativeWindow(), GA_ROOT); } WRL::ComPtr @@ -95,6 +101,21 @@ void SharePlusWindowsPlugin::HandleMethodCall( std::unique_ptr> result) { // Handle the share method. if (method_call.method_name().compare(kShare) == 0) { + // Apps built on a bare |FlutterEngine| have no implicit view, so there is + // no window for the share dialog to be anchored to. Previously this + // dereferenced null and took the process down with it; report it as a + // catchable error instead. + if (GetWindow() == nullptr) { + result->Error(kNoWindowErrorCode, + "share_plus could not resolve a window to show the share " + "dialog in. The plugin was registered without an implicit " + "view, which is the case when the app runs on a bare " + "FlutterEngine, as with the experimental windowing API " + "(enable-windowing: true).", + nullptr); + return; + } + auto data_transfer_manager = GetDataTransferManager(); auto args = std::get(*method_call.arguments()); diff --git a/packages/share_plus/share_plus/windows/share_plus_windows_plugin.h b/packages/share_plus/share_plus/windows/share_plus_windows_plugin.h index ca7d2aaa14..1bb78b5910 100644 --- a/packages/share_plus/share_plus/windows/share_plus_windows_plugin.h +++ b/packages/share_plus/share_plus/windows/share_plus_windows_plugin.h @@ -47,6 +47,10 @@ class SharePlusWindowsPlugin : public flutter::Plugin { static constexpr auto kShare = "share"; //static constexpr auto kShareFiles = "shareFiles"; + // Returned by |share| when the plugin was registered without an implicit + // view, so there is no window to anchor the share dialog to. + static constexpr auto kNoWindowErrorCode = "no_implicit_view"; + HWND GetWindow(); WRL::ComPtr GetDataTransferManager();