Original address: medium.com/flutter-com…

Original author: medium.com/rafalwacho…

Published: April 16, 2021 – read in 4 minutes

Thanks to Flutter’s support for the Linux desktop, we can now look forward to many more beautiful applications for this great platform. However, when you want to write a custom plug-in to extend the functionality of your application, you need to write some C code. I couldn’t find instructions anywhere on how to do this, so here’s how to set up Visual Studio Code to develop and debug the Flutter plugin for Linux.

Before you begin

I hope you have Linux build tools and VS Code installed, but if not, here are two links on how to do it.

Flutter. Dev/desktop# add…

code.visualstudio.com/

Create your plug-in from a template

First, you need to create your project. Call this command if you have a Flutter in your PATH variable.

flutter create - org com.example - platforms=linux myapp
Copy the code

Where myApp is the name of the plug-in. (You can also add more platforms after the comma.)

Compile the project first

On Linux, Flutter uses CMake’s tools when compiling embedder code and launches the GTK window with our application. To debug native code, you first need to run a Flutter project. It makes a tool to create the necessary symbolic links for the Cmake project. Inside myApp/Example, run our favorite commands.

flutter run
Copy the code

After that, exit the application.

Open the main project

Now open the v Code inside our roots in the newly created plug-in directory (myapp /), next to install Microsoft c + + plugin — marketplace.visualstudio.com/items?itemN… . You’ll be asked to join the Insider channel for the plugin, which you can do if you want to get the new features rolling out faster.

CMake integration

Flutter uses the Clang compiler, so choose Clang.

You will also be asked if you want to integrate CMake for your project, select Yes, and then click on the Locate CMake file.

Select “Locate” and navigate to the file.

You will be asked the location of the cmakelist.txt file and asked to select the one inside.

myapp/example/linux/CMakeLists.txt

Do not select myApp /Linux or IntelliSense will not work.

The location of the CMakeLists. TXT

Now restart with the code.

At this point, the code should be done.

Set the debug

Debugging with print statements is not the best way to analyze your code, so we need to configure a debugger that is appropriate.

Sets the location of the file

Create subfolders in the root directory (myapp/). Vscode (note) and create a file called c_cpp_properties.json inside — this is where vscode’s C++ plugin reads the configuration.

{
  "configurations": [{"name": "Linux"."includePath": [
        "${workspaceFolder}/**"]."defines": []."compilerPath": "/usr/bin/clang"."cStandard": "c17"."cppStandard": "c++14"."intelliSenseMode": "linux-clang-x64"."compileCommands": "${workspaceFolder}/build/compile_commands.json"."configurationProvider": "ms-vscode.cmake-tools"}]."version": 4
}
Copy the code

You will also need a settings.json file for the cmake configuration.

{
  "cmake.sourceDirectory": "${workspaceFolder}/example/linux"."cmake.configureOnOpen": true."files.associations": {
    "cstring": "cpp"}}Copy the code

Finally, create launch.json with two launch configurations.

{
  "version": "0.2.0"."configurations": [{"name": "Launch Flutter"."cwd": "example"."request": "launch"."type": "dart"
    },
    {
      "name": "Debug native"."type": "cppdbg"."request": "launch"."program": "${workspaceFolder}/example/build/linux/x64/debug/bundle/myapp_example"."cwd": "${workspaceFolder}"}}]Copy the code

You need to adjust the name of the executable we created with the flutter run command. Take a look at this part of JSON.

{
  "program": "${workspaceFolder}/example/build/linux/x64/debug/bundle/myapp_example"
}
Copy the code

The name of our executable is myapp_example. If you create a project with a different name, it might be different. If you are running a different version of Flutter, the path may be slightly different. You can check for the correct binary by simply running it.

Run the code

We created two startup configurations, one to run flutter with darts as usual, and the second to run the binary created with the C++ debugger. One caveat to this approach is that if you change something in the dart code, you will have to rerun the flutter run.

And you’re done!

Is it perfect? No, but is it better than printing variables to the console? Yes.

The closing

Hope you found this useful and I hope TO see more Flutter add-ons on Linux soon.

Happy coding!


Translation via www.DeepL.com/Translator (free version)