In Flutter it is possible to develop plugins for executing platform specific code. For example on a windows host it is possible to invoke c++ code from the Flutter client with:
final int result = await platform.invokeMethod('getBatteryLevel');
At the windows host you can send a reply to this call for example with:
channel.SetMethodCallHandler(
[](const flutter::MethodCall<>& call,
std::unique_ptr<flutter::MethodResult<>> result) {
if (call.method_name() == "getBatteryLevel") {
int battery_level = GetBatteryLevel();
if (battery_level != -1) {
result->Success(battery_level);
}
else {
result->Error("UNAVAILABLE", "Battery level not available.");
}
}
else {
result->NotImplemented();
}
});
I want to go the other direction. The following code sends the battery level from the host to the Flutter client:
int battery_level = GetBatteryLevel();
method_channel_->InvokeMethod(
"sendBatteryLevel",
std::make_unique<flutter::EncodableValue>(flutter::EncodableMap{
{flutter::EncodableValue("batteryLevel"), flutter::EncodableValue(battery_level)},
}));
//string answer = await a string answer from method_channel_
But how can I send a return value back from the client to the host? I want to answer this call on the client, for example like
_handleMethodCall(MethodCall call) async {
switch (call.method) {
case "batteryLevel":
final args = call.arguments as Map;
final batteryLevel= args['batteryLevel'] as int;
//Send return value
call.answer('Thank you for informing me!'); //not working, this is what I want to do
break;
}
}
The method InvokeMethod(...)
from flutter::MethodChannel has a flutter::MethodResult parameter. But i didn't manage to to call it properly in order to receive a return value for the call from the flutter client
Update: I tried smorgans suggestion already using this client code:
_handleMethodCall(MethodCall call) async {
switch (call.method) {
case "batteryLevel":
final args = call.arguments as Map;
final batteryLevel= args['batteryLevel'] as int;
//Send return value
return 'Thank you for informing me!'; //I want to receive this string at the C++ host code
}
}
My problem is that i don't get the C++ host code working to receive the answer. I tried the following:
int battery_level = GetBatteryLevel();
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> resultPointer; // How do I declare this properly?
method_channel_->InvokeMethod(
"sendBatteryLevel",
std::make_unique<flutter::EncodableValue>(flutter::EncodableMap{
{flutter::EncodableValue("batteryLevel"), flutter::EncodableValue(battery_level)},
}), resultPointer);
//std::string answer = exctractAnswerFromMethodResult(resultPointer); // how do I do this?
I tried to receive the answer as shown above, but i didn't manage to pass the MethodResult parameter properly to method_channel_->InvokeMethod. The code from above results in the this compiler error: