init method

void init()

Call this method immediately after constructing an instance.

It is intentional that no Future is returned, because the Flutter UI should not block on expensive initialization. Calling any async method of the class will ensure that the initialization has completed.

Implementation

void init() {
  if (Platform.isAndroid) {
    AndroidInAppWebViewController.setWebContentsDebuggingEnabled(true);
  }
  _headlessInAppWebView = HeadlessInAppWebView(
    onConsoleMessage: (controller, consoleMessage) {
      print("CONSOLE MESSAGE: ${consoleMessage.message}");
    },
    initialUrlRequest: URLRequest(url: _jsBridgeUrl),
    onLoadStop: (controller, url) {
      _webViewController = controller;
      controller.addJavaScriptHandler(
        handlerName: 'open',
        callback: (args) {
          _browser.open(url: Uri.parse(args[0].toString()));
        },
      );
      controller.addJavaScriptHandler(
        handlerName: 'asyncResult',
        callback: (args) {
          _asyncResultsController.add(args);
        },
      );
      controller.evaluateJavascript(source: """
        window.postMessage({
          messageType: 'Capsule#init',
          arguments: {
            environment: '${environment.name.toUpperCase()}',
            apiKey: '$apiKey',
          }
        });
      """);
      _initCompleter.complete();
    },
  );
  _headlessInAppWebView!.run();
}