rmawatson/flutter_isolate

flutter_inappwebview is not working.

cdoco opened this issue · 2 comments

cdoco commented

I want to HeadlessInAppWebView is used in flutter_isolate, but it does not work.

Example code

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

void main() async {
  runApp(
    MaterialApp(
      home: Scaffold(
        body: Center(child: Text("hello word")),
      ),
    ),
  );

  await FlutterIsolate.spawn(isolateHeadlessWebView, 'https://google.com');
}

Future<void> isolateHeadlessWebView(String url) async {
  final _webview = HeadlessInAppWebView(
    initialUrlRequest: URLRequest(url: Uri.parse(url)),
    onConsoleMessage: (controller, consoleMessage) {
      print('Console Message: ${consoleMessage.message}');
    },
    onLoadStop: (controller, url) async {
      print('onLoadStop: $url');
    },
  );

  await _webview.run();
}

Error received

E/MethodChannel#com.pichillilorenzo/flutter_headless_inappwebview(15275): Failed to handle method call
E/MethodChannel#com.pichillilorenzo/flutter_headless_inappwebview(15275): java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
E/MethodChannel#com.pichillilorenzo/flutter_headless_inappwebview(15275): 	at com.pichillilorenzo.flutter_inappwebview.in_app_webview.FlutterWebView.<init>(FlutterWebView.java:51)
E/MethodChannel#com.pichillilorenzo/flutter_headless_inappwebview(15275): 	at com.pichillilorenzo.flutter_inappwebview.headless_in_app_webview.HeadlessInAppWebViewManager.run(HeadlessInAppWebViewManager.java:69)
E/MethodChannel#com.pichillilorenzo/flutter_headless_inappwebview(15275): 	at com.pichillilorenzo.flutter_inappwebview.headless_in_app_webview.HeadlessInAppWebViewManager.onMethodCall(HeadlessInAppWebViewManager.java:58)
E/MethodChannel#com.pichillilorenzo/flutter_headless_inappwebview(15275): 	at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:233)
E/MethodChannel#com.pichillilorenzo/flutter_headless_inappwebview(15275): 	at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:84)
E/MethodChannel#com.pichillilorenzo/flutter_headless_inappwebview(15275): 	at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:865)
E/MethodChannel#com.pichillilorenzo/flutter_headless_inappwebview(15275): 	at android.os.MessageQueue.nativePollOnce(Native Method)
E/MethodChannel#com.pichillilorenzo/flutter_headless_inappwebview(15275): 	at android.os.MessageQueue.next(MessageQueue.java:335)
E/MethodChannel#com.pichillilorenzo/flutter_headless_inappwebview(15275): 	at android.os.Looper.loop(Looper.java:193)
E/MethodChannel#com.pichillilorenzo/flutter_headless_inappwebview(15275): 	at android.app.ActivityThread.main(ActivityThread.java:8056)
E/MethodChannel#com.pichillilorenzo/flutter_headless_inappwebview(15275): 	at java.lang.reflect.Method.invoke(Native Method)
E/MethodChannel#com.pichillilorenzo/flutter_headless_inappwebview(15275): 	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:656)
E/MethodChannel#com.pichillilorenzo/flutter_headless_inappwebview(15275): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:967)
E/flutter (15275): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: PlatformException(error, Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference, null, java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
E/flutter (15275): 	at com.pichillilorenzo.flutter_inappwebview.in_app_webview.FlutterWebView.<init>(FlutterWebView.java:51)
E/flutter (15275): 	at com.pichillilorenzo.flutter_inappwebview.headless_in_app_webview.HeadlessInAppWebViewManager.run(HeadlessInAppWebViewManager.java:69)
E/flutter (15275): 	at com.pichillilorenzo.flutter_inappwebview.headless_in_app_webview.HeadlessInAppWebViewManager.onMethodCall(HeadlessInAppWebViewManager.java:58)
E/flutter (15275): 	at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:233)
E/flutter (15275): 	at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:84)
E/flutter (15275): 	at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:865)
E/flutter (15275): 	at android.os.MessageQueue.nativePollOnce(Native Method)
E/flutter (15275): 	at android.os.MessageQueue.next(MessageQueue.java:335)
E/flutter (15275): 	at android.os.Looper.loop(Looper.java:193)
E/flutter (15275): 	at android.app.ActivityThread.main(ActivityThread.java:8056)
E/flutter (15275): 	at java.lang.reflect.Method.invoke(Native Method)
E/flutter (15275): 	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:656)
E/flutter (15275): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:967)
E/flutter (15275): )
E/flutter (15275): #0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:607:7)
E/flutter (15275): #1      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:156:18)
E/flutter (15275): <asynchronous suspension>
E/flutter (15275): #2      HeadlessInAppWebView.run (package:flutter_inappwebview/src/in_app_webview/headless_in_app_webview.dart:157:5)
E/flutter (15275): <asynchronous suspension>
E/flutter (15275): #3      isolateHeadlessWebView (package:parsing/main.dart:28:3)
E/flutter (15275): <asynchronous suspension>
E/flutter (15275): 
cdoco commented

I tracked the code because plugin.activity is null.

I guess flutter_inappwebview needs to rely on UI interaction and may need to implement ActivityAware.
https://github.com/flutter/flutter/wiki/Experimental:-Create-Flutter-Plugin#uiactivity-plugin

@cdoco unfortunately isolates are not attached to an Activity (see #71 (comment) for further details) and I'm not sure if there's a catch-all solution for doing so (arbitrarily spawning a new Activity for every isolate would be a bad idea).

I'll keep this issue open to investigate further, but I wouldn't hold my breath for a solution.