flutterscript
一个可嵌入的Flutter应用程序解释器
入门
import "package:flutterscript/flutterscript.dart";
main() async {
FlutterScript interpreter = await FlutterScript.create();
await interpreter.eval('"Hello World"'); //> "Hello World"
}
您可以使用defn方法将dart函数嵌入到解释器中
await interpreter.defn("loud", (DartArguments arguments) {
String input = arguments.positional.first.toString();
return "${input.toUpperCase()}!";
});
await interpreter.eval('(loud "Hello World")'); //> "HELLO WORLD!";
要将一个类嵌入到解释器中,您可以使用defClass方法,它接受一个构造函数和一个类的方法列表。
await interpreter.defClass("Text", (DartArguments args) => Text(args[0]), {
"data": (text, __) => text.data,
"toString": (text, __) => "Text(${text.data})"
})
await interpreter.eval('(setq text (Text "Hello World"))');
await interpreter.eval('(-> text data)') //> "Hello World";
await interpreter.eval('(-> text toString)') //> "Text(Hello World)";
门也是双向的。您不仅可以将dart函数嵌入到FlutterScript中并在FlutterScript代码中调用它们,还可以将FlutterScript函数带入Dart并在Dart代码中调用它们。
FlutterScriptFn add = await interpreter.eval('(=> (x y) (+ x y))');
add([10, 7]); //=> 17
开发
$ flutter packages get
$ flutter test
