Skip to content

Commit ed1922e

Browse files
authored
fix(android): add null check for textInputPlugin in FlutterView (#180386)
Prevents NPE in onProvideAutofillVirtualStructure when attachToEngineAutomatically is false and autofill is triggered before manual engine attachment. Added null safety check with early return and debug warning. Maintains backward compatibility. Fixes: /issues/149792#180383 <!-- Thanks for filing a pull request! Reviewers are typically assigned within a week of filing a request. To learn more about code review, see our documentation on Tree Hygiene: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md --> *Replace this paragraph with a description of what this PR is changing or adding, and why. Consider including before/after screenshots.* *List which issues are fixed by this PR. You must list at least one issue. An issue is not required if the PR fixes something trivial like a typo.* *If you had to change anything in the [flutter/tests] repo, include a link to the migration guide as per the [breaking change policy].* ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [ ] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
1 parent b7b2ec7 commit ed1922e

2 files changed

Lines changed: 42 additions & 2 deletions

File tree

engine/src/flutter/shell/platform/android/io/flutter/embedding/android/FlutterView.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1594,12 +1594,20 @@ private void sendViewportMetricsToFlutter() {
15941594
@Override
15951595
public void onProvideAutofillVirtualStructure(@NonNull ViewStructure structure, int flags) {
15961596
super.onProvideAutofillVirtualStructure(structure, flags);
1597-
textInputPlugin.onProvideAutofillVirtualStructure(structure, flags);
1597+
// Defensive null check to prevent NPE when textInputPlugin is not yet initialized
1598+
// (e.g., when attachToEngineAutomatically is false).
1599+
if (textInputPlugin != null) {
1600+
textInputPlugin.onProvideAutofillVirtualStructure(structure, flags);
1601+
}
15981602
}
15991603

16001604
@Override
16011605
public void autofill(@NonNull SparseArray<AutofillValue> values) {
1602-
textInputPlugin.autofill(values);
1606+
// Defensive null check to prevent NPE when textInputPlugin is not yet initialized
1607+
// (e.g., when attachToEngineAutomatically is false).
1608+
if (textInputPlugin != null) {
1609+
textInputPlugin.autofill(values);
1610+
}
16031611
}
16041612

16051613
@Override

engine/src/flutter/shell/platform/android/test/io/flutter/embedding/android/FlutterViewTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,14 @@
3939
import android.os.Build;
4040
import android.provider.Settings;
4141
import android.util.DisplayMetrics;
42+
import android.util.SparseArray;
4243
import android.view.DisplayCutout;
4344
import android.view.RoundedCorner;
4445
import android.view.Surface;
4546
import android.view.View;
47+
import android.view.ViewStructure;
4648
import android.view.WindowInsets;
49+
import android.view.autofill.AutofillValue;
4750
import android.widget.FrameLayout;
4851
import androidx.core.util.Consumer;
4952
import androidx.test.core.app.ActivityScenario;
@@ -59,6 +62,7 @@
5962
import io.flutter.embedding.engine.systemchannels.SettingsChannel;
6063
import io.flutter.plugin.platform.PlatformViewsController;
6164
import io.flutter.plugin.platform.PlatformViewsController2;
65+
import java.lang.reflect.Field;
6266
import java.lang.reflect.Method;
6367
import java.util.Arrays;
6468
import java.util.Collections;
@@ -1464,4 +1468,32 @@ public int getWindowSystemUiVisibility() {
14641468
return View.SYSTEM_UI_FLAG_FULLSCREEN;
14651469
}
14661470
}
1471+
1472+
/**
1473+
* Test that autofill methods do nothing when TextInputPlugin is null. This verifies that no
1474+
* NullPointerException is thrown and the plugin methods are not called.
1475+
*/
1476+
@Test
1477+
public void autofill_doesNothingWhenTextInputPluginIsNull() throws Exception {
1478+
// Setup: Create FlutterView without attaching to engine
1479+
// This simulates the case where textInputPlugin is null (e.g., when
1480+
// attachToEngineAutomatically is false)
1481+
FlutterView flutterView = new FlutterView(ctx);
1482+
1483+
// Verify textInputPlugin is null (not initialized)
1484+
Field textInputPluginField = FlutterView.class.getDeclaredField("textInputPlugin");
1485+
textInputPluginField.setAccessible(true);
1486+
assertNull(textInputPluginField.get(flutterView));
1487+
1488+
// Test onProvideAutofillVirtualStructure - should not throw NPE
1489+
ViewStructure structure = mock(ViewStructure.class);
1490+
int flags = 0;
1491+
flutterView.onProvideAutofillVirtualStructure(structure, flags);
1492+
// No exception should be thrown
1493+
1494+
// Test autofill - should not throw NPE
1495+
SparseArray<AutofillValue> values = mock(SparseArray.class);
1496+
flutterView.autofill(values);
1497+
// No exception should be thrown
1498+
}
14671499
}

0 commit comments

Comments
 (0)