Skip to content
This repository has been archived by the owner on Mar 14, 2023. It is now read-only.

error: LateInitializationError: Field 'connector' has not been initialized. #36

Closed
nank1ro opened this issue Sep 20, 2021 · 4 comments
Closed

Comments

@nank1ro
Copy link

nank1ro commented Sep 20, 2021

Hello,

I followed your example for running JS code from dart:
I've added this code:

 return WebViewX(
      width: 320,
      height: 500,
      initialContent:
          'someUrl',
      onWebViewCreated: (controller) => webviewController = controller,
      onPageFinished: (_) {
        webviewController
            .callJsMethod('testPlatformSpecificMethod', <dynamic>['Hi']);
      },
      jsContent: const {
        EmbeddedJsContent(
          webJs:
              "function testPlatformSpecificMethod(msg) { CaptchaMessenger('Web callback says: ' + msg) }",
          mobileJs:
              "function testPlatformSpecificMethod(msg) { CaptchaMessenger.postMessage('Mobile callback says: ' + msg) }",
        ),
      },
      dartCallBacks: {
        DartCallback(callBack: recaptchaCallback, name: 'CaptchaMessenger')
      },
    );

But when I run this code I always get this error:
error: LateInitializationError: Field 'connector' has not been initialized..

Additional info:

  • I'm running the app on Web
@adrianflutur
Copy link
Owner

adrianflutur commented Sep 20, 2021

Hi, I found out where the problem is coming from

view/impl/web.dart:164

...
if (widget.initialSourceType == SourceType.html ||
    widget.initialSourceType == SourceType.urlBypass ||
    (widget.initialSourceType == SourceType.url &&
        widget.initialContent == 'about:blank')) {
   // This function sets the `connector` everytime the page is loaded
  _connectJsToFlutter(then: _callOnWebViewCreatedCallback);
} else {
  // Here the `connector` doesn't get set, so you end up getting
  // error: LateInitializationError: Field 'connector' has not been initialized
  //
  // We reach this part of the code when the `sourcetype` is url
  // and the url is not `about:blank`
  _callOnWebViewCreatedCallback();
}
...

As you can see inside the else block, the connector doesn't get set and because of that, it ends up not being initialized inside the controller.

Now, there's a small problem here: The connector cannot be set when using SourceType.url and the URL is not about:blank. This is noted here #27, precisely:

Calling any method that will interact with the page's source (i.e. call JS methods, callbacks, execute JS etc) from the controller only works when using SourceType.urlBypass or SourceType.html. This is because only in theese 2 situations the package can actually inject that JS code inside the page source. When using SourceType.url, iframe's content does not actually belong to us - it is loaded from a different domain so we're not allowed to do that.

I could add a partial workaround for this, such that the connector actually becomes the browser page, and not the iframe page. This would mean that you would have access to the browser's window object, not the iframe's one. But I'm not sure this would really solve anything, as you would get a NoSuchMethodError when calling any methods because they won't exist in the browser's window object.
(You can check out this behaviour on another branch here: https://github.com/adrianflutur/webviewx/tree/attempt-unify-url-html-controller-init)

A solution would be to change the sourceType to SourceType.urlBypass. This will allow the package to inject EmbeddedJsContent and DartCallbacks inside the page, at the cost of using a proxy server every time in order to fetch the page's HTML source.

@nank1ro
Copy link
Author

nank1ro commented Sep 22, 2021

thank you for your response. I have to try using SourceType.urlBypass and I'll let you know if this fits my needs

@adrianflutur
Copy link
Owner

Closing this for now, please open another issue if you encounter any more issues.

@Ahmadre
Copy link

Ahmadre commented Mar 22, 2022

@adrianflutur why is this important issue closed? The most important function in this whole plugin is NOT WORKING!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants