我有一个自定义颤振插件。从 Flutter 应用程序中,我在通道上调用 setMethodCallHandler,用于在本机上调用方法。Flutter 到本地通信与响应一起工作。但是反过来是行不通的,在本机端调用方法。本机尝试调用方法,但无法找到回调,因为响应始终“未实现”
通道是在 Flutter 插件中定义的,我在 app 初始化时设置了 methodHandler。
VpvpBaseModulePlugin.dart
static const platformNative = const MethodChannel('com.example.test/native_channel');
static void setMethodCallHandler(Future<dynamic> handler(MethodCall call)){
channel.setMethodCallHandler(handler);
}
颤振应用
Future<dynamic> handler(MethodCall methodCall) async {
switch (methodCall.method) {
case 'callMe':
print("***** callMe: ${methodCall.arguments}");
break;
}
}
VpvpBaseModulePlugin.setMethodCallHandler(handler);
本地调用:
public lateinit var channel : MethodChannel
class VpvpBaseModulePlugin: FlutterPlugin, MethodCallHandler{
override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
flutterEngine = flutterPluginBinding.flutterEngine
channel = MethodChannel(flutterPluginBinding.getFlutterEngine().getDartExecutor(), "com.example.test/native_channel")
channel.setMethodCallHandler(this)
context = flutterPluginBinding.applicationContext
}
}
Handler(Looper.getMainLooper()).post {
val arguments = HashMap<String, Any>()
arguments["errorCode"] = error.toString();
channel.invokeMethod("callMe", arguments)
}
从flutter应用程序的本机模块调用调用有效..但它们不适用于插件的本机