injectJavascriptFileFromUrl method Null safety

Future<void> injectJavascriptFileFromUrl(
  1. {required Uri urlFile,
  2. ScriptHtmlTagAttributes? scriptHtmlTagAttributes}
)

Injects an external JavaScript file into the WebView from a defined url.

scriptHtmlTagAttributes represents the possible the <script> HTML attributes to be set.

NOTE: This method shouldn't be called in the WebView.onWebViewCreated or WebView.onLoadStart events, because, in these events, the WebView is not ready to handle it yet. Instead, you should call this method, for example, inside the WebView.onLoadStop event or in any other events where you know the page is ready "enough".

Implementation

Future<void> injectJavascriptFileFromUrl(
    {required Uri urlFile,
    ScriptHtmlTagAttributes? scriptHtmlTagAttributes}) async {
  assert(urlFile.toString().isNotEmpty);
  var id = scriptHtmlTagAttributes?.id;
  if (scriptHtmlTagAttributes != null && id != null) {
    _injectedScriptsFromURL[id] = scriptHtmlTagAttributes;
  }
  Map<String, dynamic> args = <String, dynamic>{};
  args.putIfAbsent('urlFile', () => urlFile.toString());
  args.putIfAbsent(
      'scriptHtmlTagAttributes', () => scriptHtmlTagAttributes?.toMap());
  await _channel.invokeMethod('injectJavascriptFileFromUrl', args);
}