Search code examples
flutterflutter-webflutter-plugin

Is there a way to add a JS file from a plugin?


I would need to add a JS library (one or more files) whenever a Flutter Web plugin is used. It's possible to edit the Index.html of the main app to include whatever the plugin needs but it isn't automatic. There should be a way to add them automatically, maybe during the registration phase, only that I can't find the way...


Solution

  • I found the solution, finally, in the shared_preferences_web plugin (not directly, it doesn't need libraries, but the idea was there):

    import 'dart:html' as html;
    
    html.document.body!.append(html.ScriptElement()
      ..src = 'assets/packages/package_name/assets/xxx.js' // ignore: unsafe_html
      ..type = 'application/javascript'
      ..defer = true);