Platform channel examples

Ayush Bherwani
Flutter
Published in
5 min readAug 29, 2020

Google Summer of Code (GSoC) 2020

Google Summer of Code is an initiative by Google to promote open source among student developers, where students pair with a mentor and work with an open source organization for three months.

This year marks the 16th year of Google Summer of Code, and the first year of the Dart organization taking part in GSoC. Many students might be thinking, that’s great, but where is Flutter?

The good news is, the projects listed include both Flutter and Dart projects. (And to be clear, all Flutter projects are written in the Dart language, but not all Dart projects use the Flutter framework.) Check out all of the project ideas on the Dart wiki.

This post showcases the final project, which demonstrates 3 different kinds of platform channels, and was written during the 3-month coding period, as well as an example of a federated plugin.

The completed Platform Channel sample demonstrates how to use different kinds of named channels to communicate with the host application, specifically:

  • How to use MethodChannel to invoke platform methods.
  • How to use EventChannel to listen to continuous value changes from the platform.
  • How to use BasicMessageChannel and MessageCodec to send messages of different types across the platform.

You can find the complete platform channel sample on GitHub.

PlatformChannel sample

I was selected to create the PlatformChannel sample app, which aims to demonstrate how to send data back and forth in messages between Dart and native code using platform channels.

Flutter is a UI framework that allows you to paint beautiful pixels on the screen using the same Dart code across platforms, but what if you want to work with native APIs to access sensor events, hardware information, or access hardware like a camera, or biometrics? Platform channels are here to help. It’s a mechanism that allows you to communicate with your native host application and access the platform-specific APIs.

MethodChannel demo

The MethodChannel demo demonstrates how to implement a MethodChannel and invoke platform methods to increment and decrement the value of a counter.

Related PR: #453

MethodChannel demo in action

EventChannel demo

The EventChannel demo demonstrates how to implement an EventChannel to listen to continuous values from the device’s Accelerometer sensor. It also shows how to test the EventChannel implementation, which gives you a deeper understanding of how EventChannel works.

Related PR: #462

EventChannel Demo in action

Platform Image demo

The Platform Image demo demonstrates how to use BasicMessageChannel with StandardMessageCodec to load an image from native assets. It also solves issue #20879: Would like an example of sending an image across PlatformChannels.

Related PR: 475

Platform Image demo in action

BasicMessageChannel & different MessageCodec demo

The BasicMessageChannel demo demonstrates how to use BasicMessageChannel and MessageCodec to move structured data across platforms. In the demo, it uses a variety of MessageCodecs to send and receive data about pets.

Related PRs: #484, #496

BasicMessageChannel Demo in action

Federated plugin sample

During my first video conference with my mentor, Andrew, he told me about an often-used quote at Google, to “always keep your wheels in the dirt”; in other words, “make sure that you aren’t stuck, but can make progress and move forward”. So, to keep my wheels in the dirt during GSoC, we decided that I would also work on a federated plugin sample. The sample aims to demonstrate how to build a federated plugin (a plugin divided into a different package for each platform implementation) to retrieve the current battery level of the device on different platforms. The sample also demonstrates how to work with platform channels to communicate with different platforms, including web and desktop. Currently, the plugin only supports Android and web. Support for Windows and Linux will be added soon.

Check out the federated_plugin sample on GitHub.

Related PRs: #497, #503, #507, #526

Check out all of my PRs for flutter/samples.

Other Contributions

Apart from working on the main project and contributing to the flutter/samples repository, I also contributed to the flutter/flutter repository, and it has been a fabulous learning journey for me. During the GSoC period, I opened 20 pull requests, of which 16 got merged and three are still in review. Check out my PRs to flutter/flutter.

Taking the first step and contributing to any open source project is often challenging. We all have encountered this situation, therefore to help the first time contributors and share my experience, I penned a blog, Contributing to Flutter: Getting Started.

Experience

It has been a great journey to work and learn under the mentorship of the Flutter team. For me, the best thing about GSoC is not the stipend but the mentorship and learning experience that I can cherish my whole life.

Mentorship

Andrew has not only been a great guide but also a friend who has helped me to improve my technical skills and with my career-related questions. He always trusted my abilities and allowed me to explore and experiment with the samples. Whenever I was stuck on a problem, he proactively helped me to find a way around, or introduced me to someone from the Flutter team who could help me. The same goes for Brett. Though not my primary mentor, he helped me whenever I reached out to him.

Learning

I am glad Andrew and I decided to follow the Test Driven Development process, which has helped me to understand a different type of software development and get a better understanding of how platform channels work internally. Working on a federated plugin sample also gave me a chance to explore win32 development.

Challenges

Challenges are what makes GSoC a great learning journey. One of the hurdles we faced was designing the WinRT/C++ API for the federated plugin sample. Though we were not able to overcome it due to some constraints, doing research about the topic helped me to explore the other paths and find an alternate solution.

That’s it for this article, I would like to thanks Andrew, Brett, and the Flutter team, who helped me to complete the samples and contribute to the Flutter SDK 🎉

About the author: Ayush is an Undergraduate student who is passionate about developing efficient Mobile apps in both Flutter and Android. Apart from Mobile Development, his interest lies in developing Mobile DApps. You can connect with him on Twitter, LinkedIn, and GitHub.

--

--

Ayush Bherwani
Flutter

Organizer Flutter Vadodara | GSoC’20 at Dart | Mobile Developer | Solidity Developer