
I was recently making a plugin for the Flutter Maps. One of the feature of the plugin, that I was developing, was to remind the user to turn on the location services whenever they accidentally turned it off. Though there were few third party packages which provided the current status of the location services, I needed one which would send me a Boolean data representing the current status when the event[of turning on or off the location] occured. After doing some research I found out that flutter provides an easy way to communicate with the native Android code with the help of channels.
There are three channels provided by Flutter.
MethodChannel
This is used to invoke methods on the native side and can return back a value and vise versa.
For eg: You can call this channel and receive the current battery status of the device.
EventChannel
An EventChannel is used when you want to stream data. This results in having a Stream on the Dart side of things and being able to feed that stream from the native side. This is useful if you want to send data every time a particular event occurs.
For eg. When the user turns on or off the location service.
BasicMessageChannel
BasicMessageChannel is used to encode and decode messages using a specified codec.
In this blog we will be using event channels that will notify us whenever the user enables or disables location service.
We will start by adding a new EventChannel to our flutter application.
const EventChannel _stream = EventChannel('locationStatusStream');Similarly we will also create the EventChannel in our MainActivity.kt file with the same name locationStatusStream
After we have set the channel on both sides, we are now ready to emit new events to the Flutter SDK. To emit our events, in our case, a boolean value, representing the status of location, we will start by creating an implementation of StreamHandler interface, this class is responsible for adding data into the platform channel that we just created through the onListen() method.
The
onListen()method is called whenever the stream is listened to from the Flutter. So, this is where we want to write the implementation for our code.
The LocationManager [at the bottom of onListen()] accepts a LocationListener interface which has various callback functions. The one we are particularly interested are the onProviderEnabled and onProviderDisabled which are called whenever the user enables or disables the location service respectively.
If you have noticed the
onListen()function, it accepts an argument of typeEventSinkwhich lets us add different events into our channel.
There are various methods provided by the EventSink but the most commonly used are success and error Here we are using the p1.success() method to add a boolean value of true when the onProviderEnabled is called and false when onProviderDisabled to the channel stream.
Once we have implemented our logic in the native code, we can now get our required data by simply listening to the channel stream.
For my use case i have requested the user to turn on the location service whenever the data received is false.
That’s it for this blog, I will be be writing blogs as I learn about flutter. Lets catch up on twitter[@igaurab]
