ensureScriptLoaded static method

Future<void> ensureScriptLoaded()

Ensures the JavaScript UMD script is loaded

Implementation

static Future<void> ensureScriptLoaded() async {
  if (_scriptLoaded) return _scriptLoadCompleter.future;

  if (!_scriptLoadCompleter.isCompleted) {
    final script = html.ScriptElement()
      ..src = 'assets/ekyc-id-web.umd.js'
      ..type = 'application/javascript' // Set correct MIME type
      ..onLoad.listen((event) {
        print('Script loaded successfully');
        print('EkycIDWeb available: ${js.context.hasProperty('EkycIDWeb')}');
        _scriptLoaded = true;
        _scriptLoadCompleter.complete();
      })
      ..onError.listen((event) {
        print('Script failed to load: $event');
        _scriptLoadCompleter
            .completeError(Exception('Failed to load ekyc-id-web.umd.js'));
      });

    html.document.head!.append(script);
  }

  return _scriptLoadCompleter.future;
}