Flutter Method Channels on Linux
Photo by Pau Sayrol on Unsplash
Hello again! Today I will continue my Flutter Linux journey and we will touch Linux integration again. Last time we managed to setup Visual Studio Code for plugin development, today we will take a closer look at Method Channels.
If you want to create a plugin with Method Channel support it’s quite easy, just generate a Flutter project from template using flutter create -t plugin --platforms=linux <name of your project>
. However, what if you don’t want to create a separate plugin and just add some custom code to your Flutter Linux application? I’ve found out it’s not straightforward so I thought that I will write this article so you won’t have to figure it out by yourself.
To not prolong it any longer, let’s just get started.
Create method channel
First we need to create codec
, binary messenger
and channel
. Next we assign a method call back to our custom method which we will create in the next step.
To do so let’s open my_application.cc
inside linux
folder and navigate to my_application_activate
function. Next we implement objects described above after plugins initialization.
In the example below, name_of_our_channel
is the method which we are calling from Dart code.
Callback function
Now let’s create callback function:
Pretty straightforward, we pass a channel, methodcall and some user data.
To check for the channel’s method name we need to use the fl_method_call_get_name
function on method_call
object. And compare it with strcmp
like so:
Method not implemented response
If method passed to channel does not exist, we need to return not implemented result to do this we need to call fl_method_call_respond
Error handling
Before we jump into arguments and custom result, let’s quickly look at error handling.
Fortunately for us, it’s very similar to not implemented method:
We can see it’s almost identical, we just create the result with fl_method_error_response_new
instead of fl_method_not_implemented_response_new
.
Fetching Dart arguments
Alright, now it’s time to write what we wanted to write from the beginning. Let’s assume that we want to send data from Dart to C++, to do that we just send a map from Dart side, but how to fetch it?
To do so, we need to call fl_method_call_get_args(FlMethodCall)
which returns a pointer to FlValue
.
Next we check if returned value is the proper type:
In example above we lookup for string, but there are other ones like int, float, bool, map. For complete list checkout Flutter Engine Documentation
Same goes for type check, to check whole list of enum go to Documentation
Returning some value
We’re almost done, we managed to handle not implemented method, errors, and getting arguments from method. What’s left is returning values back to Dart.
Fortunately for us it’s very similar to what we’ve already done, we just need to create FlMethodResponse
and put FlValue
inside. Here is an example:
Like before here is link to documentation for more value creation function
VSCode code completion + debugging
I think I should give you some reward for coming this far, so I decided to write setup for code completion and debugging in Visual Studio Code.
Before you start, C++ and Cmake plugins must be installed.
First let’s setup code completion. To do that create file named c_cpp_properties.json
inside .vscode
folder in the root of your project and put this config inside:
Check your compiler path (Flutter uses Clang) and adjust C/C++ standards if need.
To setup debugging we need to create launch configuration inside launch.json
in the same .vscode
folder. Let’s take a look at the config:
Pretty straightforward but you need to change your binary name. Also, be aware that to make it work you need to build your flutter project with flutter run
.
Closing
Thank you for reading, hopefully, you will find it useful.
Happy Coding!
The complete example can be found here.