Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PlatformException when calling controller.setSettings #2499

Open
1 of 2 tasks
Duskfen opened this issue Jan 14, 2025 · 3 comments
Open
1 of 2 tasks

PlatformException when calling controller.setSettings #2499

Duskfen opened this issue Jan 14, 2025 · 3 comments
Labels
bug Something isn't working

Comments

@Duskfen
Copy link

Duskfen commented Jan 14, 2025

Is there an existing issue for this?

  • I have searched the existing issues

Current Behavior

The Inappwebview crashes when i call controller.setSettings on android with a Platformexception. (See more details in the Stacktrace section)

I tested it with Android 14 and Android 11, flutter_inappwebview ^6.2.0-beta.2 and flutter_inappwebview ^6.1.5

The appended minimal reproducible example throws an unhandled exception, but the webview seems to work afterwards, so make sure to break on uncaught exceptions.
My real application does not work after this exception is thrown.

Expected Behavior

The plugin should not crash on android when calling setSettings

Steps with code example to reproduce

Steps with code example to reproduce

Here is a minimal reproducible example:
This is just a new created flutter app with the added Internet permission, flutter_inappwebview: ^6.1.5 dependency and following main.dart code:

import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  runApp(const MainApp());
}

class MainApp extends StatelessWidget {
  const MainApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: InAppWebView(
          initialUrlRequest: URLRequest(url: WebUri("https://flutter.dev")),
          onWebViewCreated: (controller) async {
            final InAppWebViewSettings? settings =
                await controller.getSettings();
            if (settings != null) {
              await controller.setSettings(
                settings: settings,
              );
            }
          },
        ),
      ),
    );
  }
}

Stacktrace/Logs

Stacktrace/Logs
PlatformException (PlatformException(error, Attempt to invoke virtual method 'boolean java.lang.Integer.equals(java.lang.Object)' on a null object reference, null, java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.Integer.equals(java.lang.Object)' on a null object reference
	at com.pichillilorenzo.flutter_inappwebview_android.webview.in_app_webview.InAppWebView.setSettings(InAppWebView.java:1005)
	at com.pichillilorenzo.flutter_inappwebview_android.webview.WebViewChannelDelegate.onMethodCall(WebViewChannelDelegate.java:229)
	at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:267)
	at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:292)
	at io.flutter.embedding.engine.dart.DartMessenger.lambda$dispatchMessageToQueue$0$io-flutter-embedding-engine-dart-DartMessenger(DartMessenger.java:319)
	at io.flutter.embedding.engine.dart.DartMessenger$$ExternalSyntheticLambda0.run(Unknown Source:12)
	at android.os.Handler.handleCallback(Handler.java:958)
	at android.os.Handler.dispatchMessage(Handler.java:99)
	at android.os.Looper.loopOnce(Looper.java:224)
	at android.os.Looper.loop(Looper.java:318)
	at android.app.ActivityThread.main(ActivityThread.java:8790)
	at java.lang.reflect.Method.invoke(Native Method)
	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:561)
	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1013)
))

Flutter Doctor output:

flutter doctor -v
[√] Flutter (Channel stable, 3.27.0, on Microsoft Windows [Version 10.0.26100.2605], locale de-AT)
    • Flutter version 3.27.0 on channel stable at ...
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 8495dee1fd (5 weeks ago), 2024-12-10 14:23:39 -0800
    • Engine revision 83bacfc525
    • Dart version 3.6.0
    • DevTools version 2.40.2

[√] Windows Version (Installed version of Windows is version 10 or higher)

[√] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at C:\Users\GabrielLugmayr\AppData\Local\Android\Sdk
    • Platform android-35, build-tools 34.0.0
    • ANDROID_HOME = C:\Users\GabrielLugmayr\AppData\Local\Android\Sdk
    • Java binary at: C:\Program Files\Android\Android Studio\jbr\bin\java
    • Java version OpenJDK Runtime Environment (build 17.0.9+0--11185874)
    • All Android licenses accepted.

[√] Android Studio (version 2023.2)
    • Android Studio at C:\Program Files\Android\Android Studio
    • Flutter plugin can be installed from:
       https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
       https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.9+0--11185874)

[√] VS Code (version 1.96.2)
    • VS Code at C:\Users\GabrielLugmayr\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension version 3.102.0

[√] Connected device (2 available)
    • 24069PC21G (mobile)          • 2e2b5286      • android-arm64 • Android 14 (API 34)
    • sdk gphone64 x86 64 (mobile) • emulator-5554 • android-x64   • Android 14 (API 34) (emulator)

[√] Network resources
    • All expected network resources are available.

• No issues found!

Flutter version

3.27.0

Operating System, Device-specific and/or Tool

Tested with Android API level 34 (real device + emulator) , 30 (real device)

For the rest see the flutter doctor output in the stacktrace/logs section

Plugin version

^6.2.0-beta.2, flutter_inappwebview ^6.1.5

Additional information

No response

Self grab

  • I'm ready to work on this issue!
@Duskfen Duskfen added the bug Something isn't working label Jan 14, 2025
@laishere
Copy link

I met this issue before.
You're calling setSettings with updated settings from getSettings right?
The problem is caused by this

if (newSettingsMap.get("forceDark") != null && !customSettings.forceDark.equals(newCustomSettings.forceDark)) {

customSettings.forceDark is abandoned I think, it's null. But getSettings will give you a non-null value so newSettingsMap.get("forceDark") != null is always true. And this line is executed without null check.

Solutions:

  1. Don't rely on getSettings
  2. Set forceDark to null before call setSettings

@Duskfen
Copy link
Author

Duskfen commented Jan 15, 2025

@laishere thanks for your quick reply!
Yes, as shown in the example I call setSettings with updated settings from getSettings
Thanks for your solutions, fortunately I only use it to set isInspectible to true on debug builds, so it's not a huge problem for me.

However, generally I think it would be good if one could rely on that calling setSettings with the result of getSettings will lead to no issues.

@laishere
Copy link

You are welcome, I happened to have this issue before.
And yeah, I agree with you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants