Understanding Method Channels In Flutter
About some time ago I started building Flutter apps. I loved the flutter approach to building apps. The only thing I want to caution you about is that, you need some native app building experience to have your way around Flutter easily. Jumping into flutter without any native experience, hmm you can build a few apps but if it comes to doing some things that required native platform interaction, you would be found wanting. Sometimes you have to fixe some errors that are happening at the native platform level. It just makes sense to have some native experience. Native experience, I mean some Java, Kotlin, Android, Swift, and iOS experience.
Now back to the aim of this post. In this post, I intend to take you through a step by step guide to writing some platform method channel codes. The whole idea of method channels is to allow you to make a call or invoke a Java or Kotlin code or Objective C or Swift code from within your flutter app. Simply put you a task to perform eg. Keep the screen on in your app. Something like this you can write code in Kotlin and also in Swift to perform this action. But you need to invoke these codes form your flutter app. This is where we use Method Channels. To do this in Kotlin in android and Swift is iOS, you will do something like this.
Kotlin Code To Keep Screen On
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
Swift Code To Keep Screen On
UIApplication.shared.isIdleTimerDisabled = true
To bring this code to Flutter, the two codes have to be placed in the right place. The reason is for the system to be able to register or create the channels so that they can be called when you invoke them.
In Kotlin, MainActivity.kt in the android folder is where you have to register the Android channel to invoke. For this example, we will register a channel with the name “bright.flutter.io/genericChannel”. See below in the picture where the directory is located in your flutter project.
In the MainActivity.kt make sure you add the “registerNotificationChannel” function. To do this, call the method in the “onCreate”. This means that the channel will be registered when the app is run.
<script src=”https://gist.github.com/bright2kwame/870f25723281edc40a87e54ecb33574c.js"></script>
Now we need to repeat this process for the IOS side too. In your AppDelegate.swift class. In the picture below is the directory you can find the AppDelegate.swift file.
Copy and past the right codes below in your AppDelegate.swift.
<script src=”https://gist.github.com/bright2kwame/27ebf7238480f5cdd34d311b1dd64016.js"></script>
Doing the above means the channel is registered to be invoked in the swift code. The moment the app is run, the method is registered.
Both channels are registered and ready to be invoked in your Flutter app. Anytime the channel “bright.flutter.io/genericChannel” is invoked with the name “screenOnChannel”, the screen of the application will stay awake.
To call your channel with the name, this all you need to do,
<script src=”https://gist.github.com/bright2kwame/250c4e3c62ee6511ce3481099d08eff7.js"></script>
Thank you for reading, in case you don't still understand something, just drop a comment. Don’t forget to give me some claps and follow me if you need more.