flutter_js
flutter_js copied to clipboard
How to call callFunction
I have a string function const test =()=>{ return 'test' } Please tell me how to call javascriptRuntime. callFunction. Can you give an example Thank you very much.
这是callFunction demo:
import 'dart:ffi' as FFI;
// 1.先注入 testFunc
String jsResult = javascriptRuntime.evaluate("""
const testFunc =()=>{
console.log('run testFunc');
return 'test'
}
// if (typeof MyClass == 'undefined') { // 官方demo
// var MyClass = class {
// constructor(id) {
// this.id = id;
// }
""").stringResult;
// 2. 获取testFunc在JS的实例
var testFuncRef = javascriptRuntime.evaluate('testFunc');
//3. 调用
JsEvalResult testFuncResult = javascriptRuntime.callFunction(testFuncRef.rawResult, FFI.nullptr);
String x = testFuncResult.stringResult;
print("$x");
输出:
[+21187 ms] flutter: run testFunc
[+3910 ms] flutter: test
其实直接调用最好:
javascriptRuntime.evaluate('testFunc()')
这是 callFunction 演示:
import 'dart:ffi' as FFI; // 1.先注入 testFunc String jsResult = javascriptRuntime.evaluate(""" const testFunc =()=>{ console.log('run testFunc'); return 'test' } // if (typeof MyClass == 'undefined') { // 官方demo // var MyClass = class { // constructor(id) { // this.id = id; // } """).stringResult; // 2. 获取testFunc在JS的实例 var testFuncRef = javascriptRuntime.evaluate('testFunc'); //3. 调用 JsEvalResult testFuncResult = javascriptRuntime.callFunction(testFuncRef.rawResult, FFI.nullptr); String x = testFuncResult.stringResult; print("$x");
输出:
[+21187 ms] flutter: run testFunc [+3910 ms] flutter: test
其实际直接调用最好:
javascriptRuntime.evaluate('testFunc()')
Thank you very much.
这是callFunction demo:
import 'dart:ffi' as FFI; // 1.先注入 testFunc String jsResult = javascriptRuntime.evaluate(""" const testFunc =()=>{ console.log('run testFunc'); return 'test' } // if (typeof MyClass == 'undefined') { // 官方demo // var MyClass = class { // constructor(id) { // this.id = id; // } """).stringResult; // 2. 获取testFunc在JS的实例 var testFuncRef = javascriptRuntime.evaluate('testFunc'); //3. 调用 JsEvalResult testFuncResult = javascriptRuntime.callFunction(testFuncRef.rawResult, FFI.nullptr); String x = testFuncResult.stringResult; print("$x");
输出:
[+21187 ms] flutter: run testFunc [+3910 ms] flutter: test
其实直接调用最好:
javascriptRuntime.evaluate('testFunc()')
javascriptRuntime.evaluate('testFunc()')可以成功, 使用JsEvalResult testFuncResult = javascriptRuntime.callFunction(testFuncRef.rawResult, FFI.nullptr);我这里报错 [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: type '_JSFunction' is not a subtype of type 'Pointer<NativeType>'