initialize static method

Future<void> initialize({
  1. Map<String, String?>? options,
})

Initializes the plugin. This method must be called before the first WebView creation.

options are additional options used to initialize the underlying browser (CEF; Chromium Embedded Framework). The CEF command line arguments can be specified. Specify arguments in the following format:

<String, String?>{
  'user-agent': 'UA String',
  'remote-debugging-port': '8888',
  'autoplay-policy': 'no-user-gesture-required',
}

Note that -- and = are not needed, although the usual CEF command line arguments are of the form --switch=value.

The following fundamental options are specified by default:

  • browser-subprocess-path: <browser subprocess path (determined automatically)>
  • disable-javascript-close-windows
  • ozone-platform: wayland (if XDG_SESSION_TYPE is wayland)

If options overlap with the default options, the specified options override the default options.

You do not necessarily need to wait for this method to complete. channel is resolved when this initialization is completed.

Implementation

static Future<void> initialize({Map<String, String?>? options}) async {
  _channel.setMethodCallHandler(WebViewLinuxPlatformController.onMethodCall);
  setupLogger();
  _pluginState = _PluginState.initializing;

  final Map<String, String?> defaultSwitches = {
    if (Platform.environment['XDG_SESSION_TYPE'] == 'wayland')
      'ozone-platform': 'wayland'
  };

  final Map<String, String?> essentialSwitches = {
    'browser-subprocess-path':
        '${path.dirname(Platform.resolvedExecutable)}/lib/flutter_webview_subprocess',
    // Necessary to prevent browsers from closing with window.close():
    'disable-javascript-close-windows': null,
  };

  final Map<String, String?> switches = {}
    ..addAll(defaultSwitches)
    ..addAll(essentialSwitches)
    ..addAll(options ?? {});

  final List<String> commandLineArgs = [
    // The Map in Dart iterates in key insertion order.
    for (MapEntry<String, String?> entry in switches.entries)
      entry.value != null ? '--${entry.key}=${entry.value}' : '--${entry.key}'
  ];

  log.fine('Initialize the plugin with args: $commandLineArgs');

  await _channel.invokeMethod('startCef', <String, dynamic>{
    'commandLineArgs': commandLineArgs,
  });
  _pluginState = _PluginState.initialized;
  _pluginInitDone.complete();
  log.fine('LinuxWebViewPlugin initialization done.');
}