|
4 | 4 | // The WASM module is loaded asynchronously when the plugin is first used. |
5 | 5 |
|
6 | 6 | import 'dart:async'; |
7 | | -import 'dart:html' as html; |
8 | | -import 'dart:js' as js; |
| 7 | +import 'dart:js_interop'; |
9 | 8 |
|
10 | 9 | import 'package:flutter_web_plugins/flutter_web_plugins.dart'; |
| 10 | +import 'package:web/web.dart' as web; |
11 | 11 |
|
12 | 12 | /// Web implementation of the Fula Client plugin. |
13 | 13 | /// |
@@ -56,28 +56,31 @@ class FulaClientWeb { |
56 | 56 | // They are generated by wasm-pack during the build process |
57 | 57 | // Note: The JS file is named after the Rust crate (fula_flutter) |
58 | 58 |
|
59 | | - final script = html.ScriptElement() |
| 59 | + final script = web.HTMLScriptElement() |
60 | 60 | ..src = 'fula_flutter.js' |
61 | 61 | ..type = 'module'; |
62 | 62 |
|
63 | 63 | final completer = Completer<void>(); |
64 | 64 |
|
65 | | - script.onLoad.listen((_) async { |
| 65 | + script.onload = ((web.Event event) { |
66 | 66 | // Call the init function exported by wasm-bindgen |
67 | | - try { |
68 | | - await js.context.callMethod('__wbg_init'); |
| 67 | + _initWasm().toDart.then((_) { |
69 | 68 | completer.complete(); |
70 | | - } catch (e) { |
| 69 | + }).catchError((e) { |
71 | 70 | completer.completeError('Failed to initialize WASM: $e'); |
72 | | - } |
73 | | - }); |
| 71 | + }); |
| 72 | + }).toJS; |
74 | 73 |
|
75 | | - script.onError.listen((event) { |
| 74 | + script.onerror = ((web.Event event) { |
76 | 75 | completer.completeError('Failed to load WASM script'); |
77 | | - }); |
| 76 | + }).toJS; |
78 | 77 |
|
79 | | - html.document.head!.append(script); |
| 78 | + web.document.head!.append(script); |
80 | 79 |
|
81 | 80 | return completer.future; |
82 | 81 | } |
83 | 82 | } |
| 83 | + |
| 84 | +/// JS interop for the WASM init function |
| 85 | +@JS('__wbg_init') |
| 86 | +external JSPromise<JSAny?> _initWasm(); |
0 commit comments