Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 1e145cb

Browse files
authored
Fix crash getting spell-check suggestions (#42466)
On some Samsung devices Flutter with spell-check enabled will crash when typing/moving near the ">" character. Stack trace is ``` Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.textservice.SentenceSuggestionsInfo.getSuggestionsCount()' on a null object reference at io.flutter.plugin.editing.SpellCheckPlugin.onGetSentenceSuggestions(SpellCheckPlugin.java:26) at android.view.textservice.SpellCheckerSession.lambda$handleOnGetSentenceSuggestionsMultiple$1$android-view-textservice-SpellCheckerSession(SpellCheckerSession.java:224) at android.view.textservice.SpellCheckerSession$$ExternalSyntheticLambda0.run(:4) at android.os.Handler.handleCallback(Handler.java:942) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loopOnce(Looper.java:226) at android.os.Looper.loop(Looper.java:313) at android.app.ActivityThread.main(ActivityThread.java:8747) at java.lang.reflect.Method.invoke(Method.java) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1067) ```
1 parent fc8f769 commit 1e145cb

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

shell/platform/android/io/flutter/plugin/editing/SpellCheckPlugin.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,11 @@ public void onGetSentenceSuggestions(SentenceSuggestionsInfo[] results) {
132132
ArrayList<HashMap<String, Object>> spellCheckerSuggestionSpans =
133133
new ArrayList<HashMap<String, Object>>();
134134
SentenceSuggestionsInfo spellCheckResults = results[0];
135+
if (spellCheckResults == null) {
136+
pendingResult.success(new ArrayList<HashMap<String, Object>>());
137+
pendingResult = null;
138+
return;
139+
}
135140

136141
for (int i = 0; i < spellCheckResults.getSuggestionsCount(); i++) {
137142
SuggestionsInfo suggestionsInfo = spellCheckResults.getSuggestionsInfoAt(i);

shell/platform/android/test/io/flutter/plugin/editing/SpellCheckPluginTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,4 +247,22 @@ public void onGetSentenceSuggestionsResultsWithSuccessAndNoResultsWhenSuggestion
247247

248248
verify(mockResult).success(new ArrayList<HashMap<String, Object>>());
249249
}
250+
251+
@Test
252+
public void onGetSentenceSuggestionsResultsWithSuccessAndNoResultsWhenSuggestionsAreInvalid2() {
253+
TextServicesManager fakeTextServicesManager = mock(TextServicesManager.class);
254+
SpellCheckChannel fakeSpellCheckChannel = mock(SpellCheckChannel.class);
255+
SpellCheckPlugin spellCheckPlugin =
256+
spy(new SpellCheckPlugin(fakeTextServicesManager, fakeSpellCheckChannel));
257+
MethodChannel.Result mockResult = mock(MethodChannel.Result.class);
258+
spellCheckPlugin.pendingResult = mockResult;
259+
260+
spellCheckPlugin.onGetSentenceSuggestions(
261+
new SentenceSuggestionsInfo[] {
262+
// This "suggestion" may be provided by the Samsung spell checker:
263+
null
264+
});
265+
266+
verify(mockResult).success(new ArrayList<HashMap<String, Object>>());
267+
}
250268
}

0 commit comments

Comments
 (0)