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 Channel
Mechanisms to communicate with platforms, but official documentation only provides mobile usage, as summarized here by combing through a handful of articles on the webWin32
Platform, 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
- First create one at the Flutter end
MethodChannel
Is the name of the Channel.
- Back to platform code, Win32 code Flutter has been generated in
windows
Inside the folder, is aCmake
The project. We need to edit it manuallyflutter_window.cpp
File in FlutterWindow::OnCreate
Function toFlutter Engine
registeredChannel
. 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)
- in
FlutterWindow::OnCreate
Bottom configurationChannel
Information, 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
withMethodChannel
The procedure is as follows:
- The Flutter end is set
EventChannel
- In the same
flutter_window.cpp
Inside,engine
registeredEventChannel
.
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_listen
The type is not the same!
Here, to prevent blocking, a new thread is opened to send the Event.
- Finally, on the Flutter side, simply set up the listener
Stream
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.