项目场景:
原有android项目集成flutter
环境:flutter sdk : 1.17.4,
android studio :3.6.3,
android sdk : 29
问题描述:
运行报错:
Unhandled Exception: MissingPluginException(No implementation found for method getDatabasesPath on channel com.tekartik.sqflite)
解决方案:
在android端的MainActivity中,使用以下方法进行注册
GeneratedPluginRegistrant.registerWith(flutterEngine);
例如:flutter与android进行交互时,声明一个方法来注册MethodChannel,同时调用GeneratedPluginRegistrant.registerWith(flutterEngine);方法。
public class FlutterPlugin implements MethodChannel.MethodCallHandler {
static String METHOD_CHANNEL_NAME = "method_channel";
FlutterFragment flutterFragment;
public FlutterPlugin(FlutterFragment flutterFragment) {
this.flutterFragment = flutterFragment;
}
static MethodChannel methodChannel;
public FlutterPlugin registerPlugin() {
FlutterPlugin instance = new FlutterPlugin(flutterFragment);
GeneratedPluginRegistrant.registerWith(flutterFragment.getFlutterEngine());
methodChannel =
new MethodChannel(
flutterFragment.getFlutterEngine().getDartExecutor().getBinaryMessenger(),
METHOD_CHANNEL_NAME);
methodChannel.setMethodCallHandler(instance);
return instance;
}
@Override
public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {
}
}
之后对该方法进行调用注册即可
FlutterPlugin plugin = new FlutterPlugin(flutterFragment).registerPlugin();