В основной проект добавляется следующая зависимость:
Код: Выделить всё
dependencies:
flutter:
sdk: flutter
library_project:
path: D:\****\Flutter Projects\library-project
Мне нужно получить доступ к собственному методу Java в проекте библиотеки из основного проекта.
Вот способ доступа к собственному Java из основного проекта в библиотеку. Следующая функция находится в функции класса библиотеки, я получил к ней доступ из основного проекта, импортировав класс функции.
Код: Выделить всё
static const platform = const MethodChannel('example.flutter.io/test');
static Future testMehtod() async {
try {
final int result = await platform.invokeMethod("getBatteryLevel");
} on PlatformException catch (e) {
print(e.message);
}
}
Код: Выделить всё
public class MainActivity extends FlutterActivity {
private static final String CHANNEL = "example.flutter.io/test";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GeneratedPluginRegistrant.registerWith(this);
new MethodChannel(getFlutterView(), CHANNEL).setMethodCallHandler(
new MethodCallHandler() {
@Override
public void onMethodCall(MethodCall call, Result result) {
if (call.method.equals("getBatteryLevel")) {
int batteryLevel = getBatteryLevel();
if (batteryLevel != -1) {
result.success(batteryLevel);
} else {
result.error("UNAVAILABLE", "Battery level not available.", null);
}
} else {
result.notImplemented();
}
}
});
}
}
private int getBatteryLevel() {
int batteryLevel = -1;
if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
BatteryManager batteryManager = (BatteryManager) getSystemService(BATTERY_SERVICE);
batteryLevel = batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY);
} else {
Intent intent = new ContextWrapper(getApplicationContext()).
registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
batteryLevel = (intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1) * 100) /
intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
}
return batteryLevel;
}
[ОШИБКА: flutter/shell/common/shell.cc(186)] Ошибка Dart: необработанное исключение:
E/flutter (25932): MissingPluginException (реализация метода testmethod на канале example.flutter.io/test не найдена)
E/flutter (25932): #0 MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:291:7)
E/flutter (25932):
E/flutter (25932): #1 Utils.testMethod(package:library_project/utils/utils.dart:16:41)
E/flutter (25932):
E/flutter (25932): #2 _MyHomePageState._incrementCounter. (пакет:arche_isp_dualmode/main.dart:60:13)
Мобильная версия