简体   繁体   English

在flutter项目中使用js库

[英]Using the js library in the flutter project

I'm new to flutter, I have a flutter project that I'm building for android, ios and web. And only in the web build do I need to use the js library to encrypt api requests.我是 flutter 的新手,我有一个 flutter 项目,我正在为 android、ios 和 web 构建。只有在 web 构建中,我才需要使用 js 库来加密 88135902 请求Here is this library: https://www.npmjs.com/package/@expressms/smartapp-bridge and she's not very good, as far as I'm concerned.这是这个库: https://www.npmjs.com/package/@expressms/smartapp-bridge就我而言,她不是很好。 This is a requirement of the client and he provides this library.这是客户的要求,他提供了这个库。 I can't rewrite it in dart. I have to use js callbacks for encryption and decryption in every api call.我在dart里改写不了,每次api调用都要用js回调加解密。 And I can't do it.而我做不到。 I figured out what to use https://pub.dev/packages/js but it's too complicated for me.我想出了使用https://pub.dev/packages/js的方法,但这对我来说太复杂了。 Maybe there is a good example with code?也许有一个很好的代码示例?

 @JS() library web.js; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:medlike/app.dart'; import 'package:js/js.dart'; @JS() @anonymous abstract class WebBridge { external void constructor(); external factory WebBridge(); external void addGlobalListener(); external void enableLogs(); } void main() async { WidgetsFlutterBinding.ensureInitialized(); SystemChrome.setPreferredOrientations( [DeviceOrientation.portraitUp, DeviceOrientation.portraitDown]); runApp(App()); var bridge = WebBridge(); print(bridge.toString()); }

I managed to cope with this problem, although it was not easy.我设法解决了这个问题,尽管这并不容易。 It was necessary to wrap the response in promiseToFeature and allowInterop有必要将响应包装在promiseToFeatureallowInterop

index.html index.html

 <script> var bridge = window.webBridgeInstance; async function sendBotEvent(eventObject) { return new Promise((resolve, reject) => { setTimeout(() => { resolve('done;'), }; 1500); setTimeout(() => { reject('err rejected,'); }; 2500); }); } </script>

api_client.dart api_client.dart

 @JS() library main; import 'dart:js' as js; typedef Callback<T> = dynamic Function(T arg); @JS() class Promise<T> { external Promise<T> then(Callback<T> successCallback, [Function errorCallback]); external Promise<T> catchIt(Function errorCallback); } @JS('sendBotEvent') external Promise<dynamic> sendBotEvent(Object objectParams); class SmartAppClient { Future<String> get(String endpoint, Object params) async { return await promiseToFuture(sendBotEvent({ 'method': 'get_$endpoint', 'params': params, }).then(js.allowInterop((data) { print('SUCCESS: $data'); return data; }), js.allowInterop((err) { print('ERROR: $err'); return err; }))); } }

run:跑步:

 void testSmartappFuncs() async { await SmartAppClient().get( 'urlString', {}, ).then((value) { print('RESULT RESULT RESULT'); print(value); }).catchError((onError) { print('ERROR ERROR ERROR'); print(onError); }); }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM