deep dive into the native world utilizing methodology channels – Utility Not Responding | Saga Tech

almost deep dive into the native world utilizing methodology channels – Utility Not Responding will cowl the newest and most present suggestion all however the world. entre slowly fittingly you perceive with out issue and appropriately. will addition your information skillfully and reliably

Picture by Joseph Barrientos on Unsplash


Flutter features a built-in means of speaking with the underlying platform: the so-called methodology channels.

However…wait a second…Is not Flutter a cross platform framework? So why do we have to go all the way down to the “native” world?

Nicely, typically we need to benefit from a sure HW characteristic, like digital camera or geolocation, and we will not discover a third-party plugin or library that fits our wants. Then we’ll must do all of the heavy lifting ourselves and entry the operate natively by utilizing methodology channels.


Technique channels are simply one other sort of object supplied by the Flutter API. Every channel is recognized by its Titleso each communication “bridge” between Flutter and the native platform will need to have a Distinctive identifier.

That is why we normally configure the names by combining the bundle identifier and a few suffix, for instance:

static const String PACKAGE =

static const String SUFFIX = "/openweather";

information sharing

Technique channels may be seen as a direct information stream with the native working system, permitting us to invoke strategies and ship or retrieve info.

All information exchanged over methodology channels is distributed as messages: A message is only a packet with some key-value pairs routinely serialized and deserialized by the platform.

All exchanged messages are despatched asynchronously.

System structure

Flutter methodology channel structure

When utilizing methodology channels, the Flutter framework and its underlying platform observe a shopper server structure.

The most typical state of affairs is when:

  • on the Flutter facet, we request sources (so Flutter acts as a shopper)
  • on the native facet, we carry out the required operations and serve the end result (so the native facet acts because the server)

Nevertheless, we will additionally configure the channel to alternate the roles performed by all sides.

challenge setup

Along with the dart information in our software, when utilizing methodology channels, we’ll want so as to add Android and/or ios native code thus, each in its corresponding folder.


  1. On the native facet, implement the required code
  2. On the native facet, configure the native “entry level” class
  3. On Flutter’s facet, create a technique channel
  4. On the Flutter facet, use the above object to invoke native operation

The steps required to get a technique channel up and operating are the identical for Android and iOS, however the implementation particulars change on the native facet.

The next sections take the native Android implementation for instance, implementing a technique channel to retrieve climate forecast information.

1. Native facet: implementation

To start with, we’ll outline a category known as “OpenWeatherService“, chargeable for retrieving the climate forecast from a distant server. The category implementation makes use of Kotlin coroutines:

object OpenWeatherService {
    val URL = ""

    droop enjoyable getForecast(
appId: String, 
lat: Double, 
lon: Double) : String 

//XXX: hit some API and get climate information...


2. Native facet: configure the entry level

After that, we’ll “hook” the above service class, in order that its strategies may be accessed from the native Android app.

To try this, we should:

  • register the identify of the operations we need to entry
  • Hyperlink every of those names to the corresponding implementation of the operation inside the category

In Flutter, the entry level for the underlying Android challenge is the “MainActivity.configureFlutterEngine()” methodology. So each the registration and the binding have to be performed inside that methodology:

non-public val CHANNEL = "com.bgomez.flutter_weather"
non-public val SUFFIX = "/openweather"
non-public val GET_CURRENT = "getCurrentWeather"
non-public val GET_FORECAST = "getForecast"
override enjoyable configureFlutterEngine(
   @NonNull flutterEngine: FlutterEngine) 
      .setMethodCallHandler  name, end result ->

      if (name.methodology == GET_CURRENT) 
         val res = 
               .getCurrentWeather(appId, lat, lon)
         end result.success(res)
      // 24H FORECAST
       else if (name.methodology == GET_FORECAST)     
         val res= 
               .getForecast(appId, lat, lon)
         end result.success(res)

The invocation of the tactic channel have to be performed within the Android UI Threadso we have to wrap the above snippet with some thread dealing with code:

override enjoyable configureFlutterEngine(
   @NonNull flutterEngine: FlutterEngine) 

   // Drive invocation on UI thread     
         //XXX: prev methodology channel code goes right here 

3. Flutter facet: create a technique channel

As talked about above, the Flutter framework contains the “ChannelMethod” information sort. Cases of this class characterize a communication bridge between Flutter and native:

A channel is created immediately by calling the category constructor and passing its identify as a parameter. We will wrap the operation in a manufacturing facility methodology:

  static const String PACKAGE ="...";
  static const String SUFFIX = "/climate";

  MethodChannel create() 
    return MethodChannel("$PACKAGE$SUFFIX");

After that, we use the above operate to create our occasion:

 closing channel = create();

4. Flutter facet: native methodology invocation utilizing MethodChannel

Final however not least, we have to invoke operations on the underlying platform utilizing the “MethodChannel.invokeMethod()“, which takes as parameters:

  • the native methodology identify we need to run
  • the message we need to transfer to the native facet. That is only a JSON object containing key-value pairs with the values ​​wanted to carry out the operation
closing json = 
await channel
        "lon": settings.metropolis.geo.lon,
        "appId": settings.appId

And that will be all! Our methodology channel is now prepared to speak with the underlying platform.

pattern code

As common, try this repository to entry the supply code. Write to you subsequent time!


I hope the article nearly deep dive into the native world utilizing methodology channels – Utility Not Responding provides acuteness to you and is beneficial for adjunct to your information

deep dive into the native world using method channels – Application Not Responding