准备工作
Dart 调用 JS
这里面我们使用 js
库来实现 JS 调用 Dart,首先添加依赖:
1 2
| dependencies: + js: ^0.6.4
|
在 Dart 侧定义调用方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| @JS() @anonymous class WxConfigOption { external bool? debug;
external String appId;
external num timestamp;
external String nonceStr;
external String signature;
external List<String> jsApiList;
external List<String> openTagList;
external factory WxConfigOption({ bool? debug, required String appId, required num timestamp, required String nonceStr, required String signature, required List<String> jsApiList, required List<String> openTagList, }); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| @JS() class Promise<T> { external Promise(void Function(void Function(T result) resolve, Function reject) executor); external Promise then(void Function(T result) onFulfilled, [Function onRejected]); }
@JS("flutterWeb") class FlutterWeb { external static Promise<void> configJsSdk(WxConfigOption options);
external static Promise<String> uploadImage(); }
|
在《FlutterWeb实战:04-集成微信JS-SDK提供丰富体验》中,我们介绍了如何封装微信的 JS-SDK 方法,供 Flutter 调用。
最后在 JS 侧导出了被调用方法:
1 2 3
| import * as flutterWeb from "./index.js";
window.flutterWeb = flutterWeb;
|
这样就可以在 Dart 侧调用 JS 方法了:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| Future resolveSdkSign() { final completer = Completer<void>(); FlutterWeb.configJsSdk(WxConfigOption( appId: appId, timestamp: timestamp, nonceStr: nonceStr, signature: signature, jsApiList: ['chooseImage','uploadImage'], openTagList: [ 'wx-open-launch-app', 'wx-open-launch-weapp' ] )).then(allowInterop(completer.complete), allowInterop(completer.completeError)); return completer.future; }
|
可以以这种形式调用:
配置 JS-SDK
1
| resolveSdkSign().then((_) {})
|
上传图片
1 2
| FlutterWeb.uploadImage() .then(allowInterop(completer.complete), allowInterop(completer.completeError));
|
JS 调用 Dart
1
| window.jsOnEvent("events.page.active");
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| @JS('jsOnEvent') external set _jsOnEvent(void Function(dynamic event) f);
class PlatformCallWebPlugin { static void registerWith(Registrar registrar) { final MethodChannel channel = MethodChannel( 'nicestwood.com/forest', const StandardMethodCodec(), registrar); channel.setMethodCallHandler(handleMethodHandler);
_jsOnEvent = allowInterop((dynamic event) { if (event == 'events.page.active') { } }); } }
|
参考资料