【问题标题】:Dart JS interop - Passing JS context as argumentDart JS 互操作 - 将 JS 上下文作为参数传递
【发布时间】:2020-11-06 17:26:42
【问题描述】:

如何将上下文从 Dart 传递给 JavaScript?

我正在尝试这个:

JS函数

function foo(this, (arg) {
    console.log(arg);
});

飞镖

@JS("LibJS")
library libjs;

import 'package:js/js.dart';

@JS("foo")
external dynamic foo(dynamic context, dynamic function);

void main() {
  var obj = foo(context, (arg) {});
}

【问题讨论】:

    标签: javascript flutter dart interop


    【解决方案1】:
    @JS() // if your JS call isn't LibJS.foo() then remove the string
    library libjs;
    
    import 'package:js/js.dart';
    
    @JS() // no need to add the "foo" if the name of the function is foo
    external dynamic foo(dynamic context, Function() function);
    
    void main() {
      var obj = foo(context, allowInterop((arg) {
         return 'hello';
      }));
    }
    

    【讨论】:

    • 如果解决了,请标记为答案以帮助其他人@Gustavo
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-31
    • 1970-01-01
    • 2015-07-21
    • 2019-02-16
    • 2021-01-13
    • 2021-03-22
    • 2018-04-19
    相关资源
    最近更新 更多