我正在处理PlatformChannels
尝试从Kotlin
到进行通信Flutter
。尝试实际执行来自flutter平台频道的文档中解释的内容,但方向相反:
想法是从 MainActivity.kt 类的 configureFlutterEngine 函数中调用 Flutter 函数。
为此,我在 Flutter 方面使用 main.dart(来自 Flutter 的默认示例):
class _MyHomePageState extends State<MyHomePage> {
static const platformChannel = const MethodChannel('myTestChannel');
@override
Widget build(BuildContext context) {
platformChannel.setMethodCallHandler((call){
print("Hello from ${call.method}");
return null;
});
//
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
//
//
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
],
),
),
);
}
}
在Kotlin 方面,我只是尝试在MainActivity.kt上调用颤振回调方法:
override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
GeneratedPluginRegistrant.registerWith(flutterEngine)
val channel = MethodChannel(flutterEngine.dartExecutor.binaryMessenger, "myTestChannel")
channel.invokeMethod("myTestChannel","the argument from Android")
}
但是当我运行代码时,Flutter 端没有打印任何内容。也没有崩溃或异常。