Flutter调用插件plugin
1、在主app的lib目录下某dart文件build中有一行代码调用 插件名.函数名。
2、在插件的lib目录下有一个dart文件,有如下代码:
static const MethodChannel _channel =。。。
static Future<String?> get platformVersion async {
final String? version = await _channel.invokeMethod(‘getPlatformVersion’);
return version;
}
static Future<String?> 。。。({
required double 。。。
}) async {
final String? res= await _channel.invokeMethod(“…”, {
“。。。”: 。。。
});
return res;
}
3、插件android/src/main/包名/。。 下有一个…Plugin.java文件,如下:
public class 。。。 implements FlutterPlugin, MethodCallHandler {
static final String CLASS_NAME = “。。。”;
/**
* The MethodChannel that will the communication between Flutter and native Android
* This local reference serves to register the plugin with the Flutter Engine and unregister it
* when the Flutter Engine is detached from the Activity
*/
private MethodChannel channel;
@Override
public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), “。。。”);
channel.setMethodCallHandler(this);
}
@Override
public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
channel.setMethodCallHandler(null);
}
@Override
public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
try {
switch (call.method) {
case “。。。”:
onMethod。。。Call(call, result);
break;
case “startKeyExchange”:
onMethod。。。Call(call, result);
break;
… … … … …
4、 private void onMethod。。。Call(@NonNull MethodCall call, @NonNull Result result) 可以通过:
result.success(str);返回.