From ed1922e4fa2b3b805e724cde8a57468cd3f4e56b Mon Sep 17 00:00:00 2001 From: lllgm <527639282@qq.com> Date: Wed, 11 Feb 2026 07:11:24 +0800 Subject: [PATCH] fix(android): add null check for textInputPlugin in FlutterView (#180386) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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: flutter/flutter/issues/149792、https://github.com/flutter/flutter/issues/180383 *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. [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 --- .../embedding/android/FlutterView.java | 12 +++++-- .../embedding/android/FlutterViewTest.java | 32 +++++++++++++++++++ 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/engine/src/flutter/shell/platform/android/io/flutter/embedding/android/FlutterView.java b/engine/src/flutter/shell/platform/android/io/flutter/embedding/android/FlutterView.java index 5bb17250c3c..fbfaa59265f 100644 --- a/engine/src/flutter/shell/platform/android/io/flutter/embedding/android/FlutterView.java +++ b/engine/src/flutter/shell/platform/android/io/flutter/embedding/android/FlutterView.java @@ -1594,12 +1594,20 @@ public class FlutterView extends FrameLayout @Override public void onProvideAutofillVirtualStructure(@NonNull ViewStructure structure, int flags) { super.onProvideAutofillVirtualStructure(structure, flags); - textInputPlugin.onProvideAutofillVirtualStructure(structure, flags); + // Defensive null check to prevent NPE when textInputPlugin is not yet initialized + // (e.g., when attachToEngineAutomatically is false). + if (textInputPlugin != null) { + textInputPlugin.onProvideAutofillVirtualStructure(structure, flags); + } } @Override public void autofill(@NonNull SparseArray values) { - textInputPlugin.autofill(values); + // Defensive null check to prevent NPE when textInputPlugin is not yet initialized + // (e.g., when attachToEngineAutomatically is false). + if (textInputPlugin != null) { + textInputPlugin.autofill(values); + } } @Override diff --git a/engine/src/flutter/shell/platform/android/test/io/flutter/embedding/android/FlutterViewTest.java b/engine/src/flutter/shell/platform/android/test/io/flutter/embedding/android/FlutterViewTest.java index 31a3e9cd336..8cdb8499b56 100644 --- a/engine/src/flutter/shell/platform/android/test/io/flutter/embedding/android/FlutterViewTest.java +++ b/engine/src/flutter/shell/platform/android/test/io/flutter/embedding/android/FlutterViewTest.java @@ -39,11 +39,14 @@ import android.media.ImageReader; import android.os.Build; import android.provider.Settings; import android.util.DisplayMetrics; +import android.util.SparseArray; import android.view.DisplayCutout; import android.view.RoundedCorner; import android.view.Surface; import android.view.View; +import android.view.ViewStructure; import android.view.WindowInsets; +import android.view.autofill.AutofillValue; import android.widget.FrameLayout; import androidx.core.util.Consumer; import androidx.test.core.app.ActivityScenario; @@ -59,6 +62,7 @@ import io.flutter.embedding.engine.renderer.FlutterRenderer; import io.flutter.embedding.engine.systemchannels.SettingsChannel; import io.flutter.plugin.platform.PlatformViewsController; import io.flutter.plugin.platform.PlatformViewsController2; +import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.Arrays; import java.util.Collections; @@ -1464,4 +1468,32 @@ public class FlutterViewTest { return View.SYSTEM_UI_FLAG_FULLSCREEN; } } + + /** + * Test that autofill methods do nothing when TextInputPlugin is null. This verifies that no + * NullPointerException is thrown and the plugin methods are not called. + */ + @Test + public void autofill_doesNothingWhenTextInputPluginIsNull() throws Exception { + // Setup: Create FlutterView without attaching to engine + // This simulates the case where textInputPlugin is null (e.g., when + // attachToEngineAutomatically is false) + FlutterView flutterView = new FlutterView(ctx); + + // Verify textInputPlugin is null (not initialized) + Field textInputPluginField = FlutterView.class.getDeclaredField("textInputPlugin"); + textInputPluginField.setAccessible(true); + assertNull(textInputPluginField.get(flutterView)); + + // Test onProvideAutofillVirtualStructure - should not throw NPE + ViewStructure structure = mock(ViewStructure.class); + int flags = 0; + flutterView.onProvideAutofillVirtualStructure(structure, flags); + // No exception should be thrown + + // Test autofill - should not throw NPE + SparseArray values = mock(SparseArray.class); + flutterView.autofill(values); + // No exception should be thrown + } }