problem description
small demo
Performing hot reload.
Syncing files to device OPPO R11
Reloaded 0 of 419 libraries in 401ms.
E/flutter (2440): [ERROR:flutter/shell/common/shell.cc
E/flutter (2440): MissingPluginException (No implementation found for method getBatteryLevel on channel samples.flutter.io/battery)
E/flutter (2440):-sharp0 MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:291:7)
* * I ran the official example code, and I can get the battery information correctly, but the official code is new MethodCallHandler ()
I can"t use new MethodCallHandler () in my own project so that I can newMethodChannel.MethodCallHandler () * *
dart file
the environmental background of the problems and what methods you have tried
1.
related codes
/ / Please paste the code text below (do not replace the code with pictures)
package com.example.batterylevel;
import android.os.Bundle;
import io.flutter.app.FlutterActivity;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugins.GeneratedPluginRegistrant;
import android.content.ContextWrapper;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.os.Bundle;
public class MainActivity extends FlutterActivity {
private static final String CHANNEL = "com.example.batterylevel/battery";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GeneratedPluginRegistrant.registerWith(this);
new MethodChannel(getFlutterView(),CHANNEL).setMethodCallHandler(
new MethodChannel.MethodCallHandler() {
@Override
public void onMethodCall(MethodCall call, MethodChannel.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();
}
}
}
);
}
static const platfrom = const MethodChannel("com.example.batterylevel/battery");
String _batteryLevel ="";
Future<Null> _getBatteryLevel() async{
String batteryLevel;
try{
final int result = await platfrom.invokeMethod("getBatteryLevel");
batteryLevel = "$result %";
} on PlatformException catch(e){
batteryLevel=":$e.message";
}
setState(() {
_batteryLevel =_batteryLevel;
});
}