text
stringlengths
1
372
finally, add the battery_method_call_handler function referenced
in the earlier call to fl_method_channel_set_method_call_handler.
you need to handle a single platform method, getBatteryLevel,
so test for that in the method_call argument.
the implementation of this function calls
the linux code written in the previous step. if an unknown method
is called, report that instead.
add the following code after the get_battery_level function:
<code_start>
static void battery_method_call_handler(FlMethodChannel* channel,
FlMethodCall* method_call,
gpointer user_data) {
g_autoptr(FlMethodResponse) response = nullptr;
if (strcmp(fl_method_call_get_name(method_call), "getbatterylevel") == 0) {
response = get_battery_level();
} else {
response = FL_METHOD_RESPONSE(fl_method_not_implemented_response_new());
}
g_autoptr(GError) error = nullptr;
if (!fl_method_call_respond(method_call, response, &error)) {
g_warning("Failed to send response: %s", error->message);
}
}
<code_end>
you should now be able to run the application on linux.
if your device doesn’t have a battery,
it displays ‘battery level not available’.
<topic_end>
<topic_start>
typesafe platform channels using pigeon
the previous example uses MethodChannel
to communicate between the host and client,
which isn’t typesafe. calling and receiving
messages depends on the host and client declaring
the same arguments and datatypes in order for messages to work.
you can use the pigeon package as
an alternative to MethodChannel
to generate code that sends messages in a
structured, typesafe manner.
with pigeon, the messaging protocol is defined
in a subset of dart that then generates messaging
code for android, iOS, macOS, or windows. you can find a more complete
example and more information on the pigeon
page on pub.dev.
using pigeon eliminates the need to match
strings between host and client
for the names and datatypes of messages.
it supports: nested classes, grouping
messages into APIs, generation of
asynchronous wrapper code and sending messages
in either direction. the generated code is readable
and guarantees there are no conflicts between
multiple clients of different versions.
supported languages are Objective-C, java, kotlin, c++,
and swift (with Objective-C interop).
<topic_end>
<topic_start>
pigeon example
pigeon file:
<code_start>
import 'package:pigeon/pigeon.dart';
class SearchRequest {
final string query;
SearchRequest({required this.query});
}
class SearchReply {
final string result;
SearchReply({required this.result});
}
@hostapi()
abstract class api {
@async
SearchReply search(SearchRequest request);
}
<code_end>
dart usage:
<code_start>
import 'generated_pigeon.dart';
future<void> onClick() async {
SearchRequest request = SearchRequest(query: 'test');
api api = SomeApi();
SearchReply reply = await api.search(request);
print('reply: ${reply.result}');
}
<code_end>
<topic_end>
<topic_start>
separate platform-specific code from UI code
if you expect to use your platform-specific code
in multiple flutter apps, you might consider
separating the code into a platform plugin located
in a directory outside your main application.
see developing packages for details.
<topic_end>
<topic_start>
publish platform-specific code as a package
to share your platform-specific code with other developers
in the flutter ecosystem, see publishing packages.
<topic_end>
<topic_start>