flutter_js icon indicating copy to clipboard operation
flutter_js copied to clipboard

How to call callFunction

Open lsdlh opened this issue 2 years ago • 3 comments

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.

lsdlh avatar Nov 28 '22 06:11 lsdlh

这是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()')

zhutc avatar Nov 30 '22 04:11 zhutc

这是 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.

lsdlh avatar Dec 05 '22 08:12 lsdlh

这是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>'

lsdlh avatar Dec 05 '22 08:12 lsdlh