injectCssAndJSLibraries method

Future<void> injectCssAndJSLibraries()

injectCssAndJSLibraries which add the JS and CSS files into DOM

Implementation

Future<void> injectCssAndJSLibraries() async {
  final List<Future<void>> loading = <Future<void>>[];
  final List<web.HTMLElement> tags = <web.HTMLElement>[];

  final cssUrl = ui.assetManager.getAssetUrl(
    'packages/fluttertoast/assets/toastify.css',
  );
  final web.HTMLLinkElement css = web.HTMLLinkElement()
    ..id = 'toast-css'
    ..setAttribute("rel", "stylesheet")
    ..href = cssUrl;
  tags.add(css);

  final jsUrl = ui.assetManager.getAssetUrl(
    'packages/fluttertoast/assets/toastify.js',
  );
  final web.HTMLScriptElement script = web.HTMLScriptElement()
    ..async = true
    // ..defer = true
    ..src = jsUrl;
  loading.add(script.onLoad.first);
  tags.add(script);
  for (final web.HTMLElement tag in tags) {
    web.document.querySelector('head')!.append(tag);
  }

  await Future.wait(loading);
}