Flutter already has plenty of Chinese articles on IOS and ANDROID, but is still in BETA on the desktop. As a scrappy junior, write something to help someone in need. (Source code at end of article)

Everyone knows that Flutter providesPlatform ChannelMechanisms to communicate with platforms, but official documentation only provides mobile usage, as summarized here by combing through a handful of articles on the webWin32Platform, the use of two channels.

Dart: services.dart: services.dart: services.dart: services.dart: services.dart: services.dart: services.dart: services.dart: services.dart: services.dart

MethodChannel

  1. First create one at the Flutter endMethodChannelIs the name of the Channel.

  1. Back to platform code, Win32 code Flutter has been generated inwindowsInside the folder, is aCmakeThe project. We need to edit it manuallyflutter_window.cppFile in FlutterWindow::OnCreateFunction toFlutter EngineregisteredChannel. Why this file? Why in this position? In a future column I will write about how Flutter Win32 starts. Click “Follow” to be notified 🤭)

The contents of OnCreate are as follows:

(If the engine starts and the view renders successfully)

  1. inFlutterWindow::OnCreateBottom configurationChannelInformation, but for the sake of simplicity, let’s write our own function call.

First, introduce the necessary header files, the first being the method_channel definition, and the second the decoder.

Next, create a decoder that uses the StandardMethodCodec provided by Flutter, then get the engine’s Messenger, and finally handle the registration logic through the method_channel.h implementation of method_channel.h.

Finally, set the SetMethodCallHandler property of the flutter::MethodChannel to configure the content of the call. There are a few important points to note here. Compare with the invokeMethod MethodChannel on the Flutter side. The information returned by the Success call to result must be wrapped with a Flutter ::EncodableValue. Otherwise, the Flutter end will only receive true instead of your intended message. Error also goes into the try catch block at the end of the Flutter. The Error message is what you wrap around the Flutter ::EncodableValue.

The code for Flutter invocation is as follows:

The console output is as follows:

One more thing: don’t write blocking code in Native calls. This may cause PlatForm Thread to block and cause the UI (your Flutter end) to not render properly, i.e. the screen freezes and the mouse moves in circles.

EventChannel

EventChannel is a little bit more complicated. The MethodChannel above is that the Flutter calls Native, whereas when you go to EventChannel, it’s more like Native actively calls the Flutter. This involves some knowledge about Stream content. If you don’t understand it, you can learn from this video.

Flutter Tutorial Async-4 Stream and StreamBuilder components

withMethodChannelThe procedure is as follows:

  1. The Flutter end is setEventChannel

  1. In the sameflutter_window.cppInside,engineregisteredEventChannel.

First, import the necessary headers, then repeat the same registration steps:

And then, setevent_channel_theSetStreamHandler

If you’re curious what this SetStreamHandler is you can go to the header file, it’s not complicated, but I won’t go into it. The SetStreamHandler argument is a pointer to StreamHandlerFunctions that require on_LISTEN and on_cancel functions. (When Cancel is called, and when Success or Error is returned, you can explore the Cancel of the AnimationController.)

Attention!!!!!On_cancel and on_listenThe type is not the same!

Here, to prevent blocking, a new thread is opened to send the Event.

  1. Finally, on the Flutter side, simply set up the listenerStream

Run a screenshot

Stream, 2S an Event, Wuhu!

Source & Demo

Without further ado, the figure above is fully annotated.

GitHub:Chinouo/flutter_channel_win32

Only one platform file flutter_window. CPP was moved. Please start flutter run.