Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

将dart方法注入到js引擎后, 通过js对象调用该方法总是返回Future<dynamic> #22

Closed
jiajiawei opened this issue Oct 15, 2021 · 2 comments

Comments

@jiajiawei
Copy link

jiajiawei commented Oct 15, 2021

您好, 非常感谢作者提供的这个仓库...

我测试代码如下 , 也可以尝试我创建的测试例子 https://github.com/jiajiawei/test_flutter_qjs

var a = 1;
final engine = FlutterQjs()..dispatch();
try {
  JSInvokable setToObject =
      engine.evaluate('(key, value) => this[key] = value;');
  setToObject.invoke(['a', IsolateFunction(() => a)]);
  print(engine.evaluate('a()'));    // Instance of 'Future<dynamic>'
} catch (e) {
  print('[JsEngine] Fail to eval script. \n$e');
} finally {
  engine.port.close();
}

我想得到1,但是结果是Instance of 'Future'

我改了object.dart三处代码, 把Future 和 aysnc都去掉临时解决了. 但是我觉得这不是正常方案. 希望作者有空解决下. 感激不尽

(还有272行的invoke方法没截上)
image

@ekibun
Copy link
Owner

ekibun commented Oct 18, 2021

直接传function

var a = 1;
final engine = FlutterQjs()..dispatch();
try {
  JSInvokable setToObject =
      engine.evaluate('(key, value) => this[key] = value;');
  setToObject.invoke(['a', () => a]);
  print(engine.evaluate('a()'));
} catch (e) {
  print('[JsEngine] Fail to eval script. \n$e');
} finally {
  engine.port.close();
}

IsolateFunction 是为了多线程(isolate)模式能异步调用做的wrapper

@jiajiawei
Copy link
Author

谢谢 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants