text
stringlengths
1
372
so test for that in the call argument.
the implementation of this platform method calls
the macOS code written in the previous step. if an unknown method
is called, report that instead.
<code_start>
batteryChannel.setMethodCallHandler { (call, result) in
switch call.method {
case "getbatterylevel":
guard let level = getBatteryLevel() else {
result(
FlutterError(
code: "unavailable",
message: "battery level not available",
details: nil))
return
}
result(level)
default:
result(FlutterMethodNotImplemented)
}
}
<code_end>
you should now be able to run the application on macOS.
if your device doesn’t have a battery,
it displays ‘battery level not available’.
<topic_end>
<topic_start>
step 7: add a linux platform-specific implementation
for this example you need to install the upower developer headers.
this is likely available from your distribution, for example with:
start by opening the linux host portion of your flutter app in the editor
of your choice. the instructions below are for visual studio code with the
“c/c++” and “cmake” extensions installed, but can be adjusted for other IDEs.
launch visual studio code.
open the linux directory inside your project.
choose yes in the prompt asking: would you like to configure project "linux"?.
this enables c++ autocomplete.
open the file my_application.cc.
first, add the necessary includes to the top of the file, just
after #include <flutter_linux/flutter_linux.h:
<code_start>
#include <math.h>
#include <upower.h>
<code_end>
add an FlMethodChannel to the _MyApplication struct:
<code_start>
struct _MyApplication {
GtkApplication parent_instance;
char** dart_entrypoint_arguments;
FlMethodChannel* battery_channel;
};
<code_end>
make sure to clean it up in my_application_dispose:
<code_start>
static void my_application_dispose(GObject* object) {
MyApplication* self = MY_APPLICATION(object);
g_clear_pointer(&self->dart_entrypoint_arguments, g_strfreev);
g_clear_object(&self->battery_channel);
G_OBJECT_CLASS(my_application_parent_class)->dispose(object);
}
<code_end>
edit the my_application_activate method and initialize
battery_channel using the channel name
samples.flutter.dev/battery, just after the call to
fl_register_plugins:
<code_start>
static void my_application_activate(GApplication* application) {
// ...
fl_register_plugins(FL_PLUGIN_REGISTRY(self->view));
g_autoptr(FlStandardMethodCodec) codec = fl_standard_method_codec_new();
self->battery_channel = fl_method_channel_new(
fl_engine_get_binary_messenger(fl_view_get_engine(view)),
"samples.flutter.dev/battery", FL_METHOD_CODEC(codec));
fl_method_channel_set_method_call_handler(
self->battery_channel, battery_method_call_handler, self, nullptr);
gtk_widget_grab_focus(GTK_WIDGET(self->view));
}
<code_end>
next, add the c code that uses the linux battery APIs to
retrieve the battery level. this code is exactly the same as
you would write in a native linux application.
add the following as a new function at the top of
my_application.cc just after the G_DEFINE_TYPE line:
<code_start>
static FlMethodResponse* get_battery_level() {
// find the first available battery and report that.
g_autoptr(UpClient) up_client = up_client_new();
g_autoptr(GPtrArray) devices = up_client_get_devices2(up_client);
if (devices->len == 0) {
return FL_METHOD_RESPONSE(fl_method_error_response_new(
"unavailable", "device does not have a battery.", nullptr));
}
UpDevice* device = (updevice*)(g_ptr_array_index(devices, 0));
double percentage = 0;
g_object_get(device, "percentage", &percentage, nullptr);
g_autoptr(FlValue) result =
fl_value_new_int(static_cast<int64_t>(round(percentage)));
return FL_METHOD_RESPONSE(fl_method_success_response_new(result));
}
<code_end>