original
https://medium.com/flutter-community/using-flutter-method-channels-on-linux-2f6ae3e99653
Code
https://github.com/charafau/linux_method_channel
refer to
- https://flutter.dev/docs/development/platform-integration/platform-channels
- https://engine.chinmaygarde.com/fl__value_8cc.html
- https://engine.chinmaygarde.com/fl__value_8h.html#a1b2a2f35eb78370fbb2d1af774f6d245
- https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools
- https://marketplace.visualstudio.com/items?itemName=ms-vscode.cmake-tools
text
Flutter Method Channels on Linux
Linux-based Flutter method channel
meet again! Today I will continue my Flutter Linux journey, and we will touch Linux integration again. Last time we managed to set up Visual Studio Code for plug-in development, today we will further study Method Channels.
If you want to create a plug-in with method channel support, it is very simple, just use flutter create \-t plugin \--platforms=linux <name of your project>
to generate a Flutter project from the template. But what if you don't want to create a separate plugin, just add some custom code to the Flutter Linux application? I found this is not easy, so I think I will write this article so that you don't have to solve it yourself.
In order not to delay any further, let's get started.
Create method channel
First, we need to create codecs codec
, binary messenger
binary messenger and channel. Next, we will assign a method call to the custom method, which we will create in the next step.
For this, let's open my my_application.cc
. Cc in the linux
folder and navigate to my my_application_activate
function. Next, we implement the objects described above after the plugin is initialized.
In the example below, name_of_our_channel
is the method we call from Dart code.
Callback
Now let's create a callback function:
Very simple, 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:
In order to check the method name of the channel, we need to use the fl_method_call_get_name
function method_call
Then compare it with strcmp
, like this:
Method does not implement response
If the method passed to the channel does not exist, we need to return an unimplemented result to do this, we need to call fl_method_call_respond
Error handling
Before we start discussing parameters and custom results, let's take a quick look at error handling.
Fortunately, it is very similar to the unimplemented method:
We can see that it is almost the same, we just use fl_method_error_response_new
create results, rather than fl_method_not_implemented_response_new
.
Get Dart parameters
Okay, now it's time to write what we want to write from scratch. Let us suppose that we want to send data from Dart to C++. In order to do this, we only need to send a map from Dart, but how to get it?
To do this, we need to call fl_method_call_get_args(FlMethodCall)
, which returns a pointer to FlValue
.
Next we check whether the returned value is of the correct type:
In the above example, we are looking for strings, but there are others, such as int, float, bool, map. For the complete list check Flutter Engine Documentation
The same is true for type checking, check the entire list of enumerations to Documentation
Return some value
We are almost done, we managed to deal with unimplemented methods, errors and get parameters from methods. The rest is to return the value of Dart.
Fortunately for us, this is very similar to what we have already done, we just need to create FlMethodResponse
and put FlValue
into it. Below is an example:
As before, there is more value creation function of document links Link to More value for Documentation Creation function
Code completion + debugging
I think I should give you some rewards, because you are here, so I decided to write the code completion and debugging setup program in Visual Studio Code.
Before starting, you must install the C++ and Cmake plug-ins.
First let's set up the code completion. Create a file c_cpp_properties.json
Json is in it. .vscode
a 0614d2a2996b38 folder in the root directory of your project, and then put this configuration file into it:
Check the compiler path (Flutter uses Clang) and adjust the c/c++ standard as needed.
In order to set up debugging, we need to create a launch configuration .vscode
folder launch.json
Let's take a look at the configuration:
Very simple, but the binary name needs to be changed. Also, be aware that to make it work, you need to build your Flutter project with flutter run
.
End of this article
Thank you for reading, and hope you will find it useful.
Happy programming!
The complete example can be found here.
https://github.com/charafau/linux_method_channel
© Cat brother
- https://ducafecat.tech/
- https://github.com/ducafecat
- WeChat group ducafecat
- b Station https://space.bilibili.com/404904528
Past
Open source
GetX Quick Start
https://github.com/ducafecat/getx_quick_start
News client
https://github.com/ducafecat/flutter_learn_news
strapi manual translation
WeChat discussion group ducafecat
Series collection
Translation
https://ducafecat.tech/categories/%E8%AF%91%E6%96%87/
Open source project
https://ducafecat.tech/categories/%E5%BC%80%E6%BA%90/
Dart programming language basics
https://space.bilibili.com/404904528/channel/detail?cid=111585
Getting started with Flutter zero foundation
https://space.bilibili.com/404904528/channel/detail?cid=123470
Flutter actual combat from scratch news client
https://space.bilibili.com/404904528/channel/detail?cid=106755
Flutter component development
https://space.bilibili.com/404904528/channel/detail?cid=144262
Flutter Bloc
https://space.bilibili.com/404904528/channel/detail?cid=177519
Flutter Getx4
https://space.bilibili.com/404904528/channel/detail?cid=177514
Docker Yapi
https://space.bilibili.com/404904528/channel/detail?cid=130578
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。