From 59946da9035a15f0a70515c3564b1d3e40f6d77a Mon Sep 17 00:00:00 2001 From: Jaime Wren Date: Wed, 13 Aug 2025 16:14:36 -0700 Subject: [PATCH] Resolves a "Slow operations are prohibited on EDT" exception This resolves https://github.com/flutter/flutter-intellij/issues/8448 Additional work may be needed to limit the number of read actions by the plugin, see https://github.com/flutter/flutter-intellij/issues/8242 --- src/io/flutter/sdk/FlutterSdkUtil.java | 38 +++++++++++++++++--------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/src/io/flutter/sdk/FlutterSdkUtil.java b/src/io/flutter/sdk/FlutterSdkUtil.java index 1801a32d2c..e7b31fd5fd 100644 --- a/src/io/flutter/sdk/FlutterSdkUtil.java +++ b/src/io/flutter/sdk/FlutterSdkUtil.java @@ -96,26 +96,39 @@ private static void updateKnownPaths(@SuppressWarnings("SameParameterValue") @No * Adds the current path and other known paths to the combo, most recently used first. */ public static void addKnownSDKPathsToCombo(@NotNull JComboBox combo) { - final Set pathsToShow = new LinkedHashSet<>(); - + // First, get the current path from the combo box on the EDT. final String currentPath = combo.getEditor().getItem().toString().trim(); + final Set pathsToShow = new LinkedHashSet<>(); if (!currentPath.isEmpty()) { pathsToShow.add(currentPath); } - final String[] knownPaths = getKnownFlutterSdkPaths(); - for (String path : knownPaths) { - if (FlutterSdk.forPath(path) != null) { - pathsToShow.add(FileUtil.toSystemDependentName(path)); + // Now, run the slow operation (finding valid SDK paths) on a background thread. + ApplicationManager.getApplication().executeOnPooledThread(() -> { + final String[] knownPaths = getKnownFlutterSdkPaths(); + final Set validPaths = new LinkedHashSet<>(); + for (String path : knownPaths) { + // The call to forPath() is on a background thread. + if (FlutterSdk.forPath(path) != null) { + validPaths.add(FileUtil.toSystemDependentName(path)); + } } - } - //noinspection unchecked - combo.setModel(new DefaultComboBoxModel(ArrayUtil.toStringArray(pathsToShow))); + // After the slow operation is complete, switch back to the EDT to update the UI. + ApplicationManager.getApplication().invokeLater(() -> { + // This code runs on the EDT. + pathsToShow.addAll(validPaths); - if (combo.getSelectedIndex() == -1 && combo.getItemCount() > 0) { - combo.setSelectedIndex(0); - } + // Update the combo box model with the new paths. + //noinspection unchecked + combo.setModel(new DefaultComboBoxModel<>(ArrayUtil.toStringArray(pathsToShow))); + + // Select the first item if none is selected. + if (combo.getSelectedIndex() == -1 && combo.getItemCount() > 0) { + combo.setSelectedIndex(0); + } + }); + }); } @NotNull @@ -365,7 +378,6 @@ public static String parseFlutterSdkPath(String packagesFileContent) { return path; } } - return null; }