-
Notifications
You must be signed in to change notification settings - Fork 330
Commit e9b498d
(originally #8496)
This PR fixes a `NoSuchElementException` that occurs in the `DeviceSelectorAction` when IntelliJ's toolbar layout system tries to calculate component widths.
## Problem
The error manifested as:
```
java.util.NoSuchElementException: Key io.flutter.actions.DeviceSelectorAction1γγ«[...] is missing in the map.
at kotlin.collections.MapsKt__MapWithDefaultKt.getOrImplicitDefaultNullable(MapWithDefault.kt:24)
at kotlin.collections.MapsKt__MapsKt.getValue(Maps.kt:369)
at com.intellij.openapi.actionSystem.toolbarLayout.CompressingLayoutStrategyKt.calculateComponentWidths(CompressingLayoutStrategy.kt:200)
```
## Root Cause
The `getPreferredSize()` method in the anonymous JButton class was being called by IntelliJ's layout system **before** the client properties (`ICON_LABEL_KEY`, `TEXT_LABEL_KEY`, `ARROW_LABEL_KEY`) were set during component initialization. This caused the layout system to fail when trying to register the component in its internal maps because:
1. The method accessed null client properties without proper fallback handling
2. Used unsafe `Objects.requireNonNull(fm)` calls that could throw exceptions
3. The layout system couldn't determine proper component dimensions during initialization
## Solution
Enhanced the `getPreferredSize()` method with defensive programming:
- **Added fallback logic**: When client properties are null (during initialization), use the same default icons and text that would normally be used
- **Safe null checking**: Replaced `Objects.requireNonNull(fm)` with proper null checks
- **Reasonable defaults**: Provide sensible sizing estimates using `FlutterIcons.Mobile`, chevron down icon, and "No device selected" text width
```java
// Before: Unsafe access
width += Objects.requireNonNull(fm).stringWidth(text);
// After: Defensive with fallback
if (fm != null) {
width += fm.stringWidth(text);
height = Math.max(height, fm.getHeight());
}
```
## Impact
- Eliminates `NoSuchElementException` during toolbar initialization
- Maintains exact same functionality once component is fully initialized
- No performance impact - fallback logic only runs during the brief initialization phase
- More robust component that gracefully handles IntelliJ's layout timing
Fixes #8494.
1 parent 559f9b1 commit e9b498d
File tree
3 files changed
+34
-7
lines changed- src/io/flutter
- actions
3 files changed
+34
-7
lines changedOriginal file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
14 | 14 |
| |
15 | 15 |
| |
16 | 16 |
| |
17 | + | ||
17 | 18 |
| |
18 | 19 |
| |
19 | 20 |
| |
|
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
74 | 74 |
| |
75 | 75 |
| |
76 | 76 |
| |
77 | + | ||
78 | + | ||
77 | 79 |
| |
78 | 80 |
| |
79 | 81 |
| |
|
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
47 | 47 |
| |
48 | 48 |
| |
49 | 49 |
| |
50 | + | ||
51 | + | ||
50 | 52 |
| |
51 | 53 |
| |
52 | 54 |
| |
| |||
87 | 89 |
| |
88 | 90 |
| |
89 | 91 |
| |
90 | - | ||
92 | + | ||
91 | 93 |
| |
92 | - | ||
94 | + | ||
93 | 95 |
| |
94 | 96 |
| |
95 | 97 |
| |
| |||
119 | 121 |
| |
120 | 122 |
| |
121 | 123 |
| |
124 | + | ||
125 | + | ||
126 | + | ||
127 | + | ||
128 | + | ||
129 | + | ||
122 | 130 |
| |
131 | + | ||
132 | + | ||
123 | 133 |
| |
124 | - | ||
125 | - | ||
134 | + | ||
135 | + | ||
136 | + | ||
137 | + | ||
138 | + | ||
139 | + | ||
140 | + | ||
141 | + | ||
142 | + | ||
143 | + | ||
126 | 144 |
| |
127 | 145 |
| |
128 | 146 |
| |
129 | 147 |
| |
130 | 148 |
| |
131 | 149 |
| |
132 | 150 |
| |
151 | + | ||
152 | + | ||
153 | + | ||
154 | + | ||
155 | + | ||
156 | + | ||
133 | 157 |
| |
134 | 158 |
| |
135 | 159 |
| |
| |||
278 | 302 |
| |
279 | 303 |
| |
280 | 304 |
| |
281 | - | ||
305 | + | ||
282 | 306 |
| |
283 | 307 |
| |
284 | 308 |
| |
285 | 309 |
| |
286 | 310 |
| |
287 | 311 |
| |
288 | 312 |
| |
289 | - | ||
313 | + | ||
290 | 314 |
| |
291 | 315 |
| |
292 | 316 |
| |
293 | - | ||
317 | + | ||
294 | 318 |
| |
295 | 319 |
| |
296 | 320 |
| |
|
0 commit comments