Hi everyone, we are gonna learn “How to make a custom plug-in for flutter?”

Let’s say you are developing a flutter application for a company you work or yourself and ran into a problem i.e., need to interact with native platform and perform some task.

There is no plugin available for flutter to perform task needed and no developer is free to write code for you.

So, here’s how we can build a custom Flutter Plugin.

Let’s start

I am using Intellij for creating project and building the plugin, you can also use other IDE like vscode, so on..

Let’s create an application which prints log messages

Log.d, Log.e, Log.w

create a new project first

create a new project of type plugin

In the command line write,

flutter create --org io.github.ponnamkarthik --template=plugin flutter_native_log

Once project is created you will find this below code in your lib folder

import 'dart:async';



import 'package:flutter/services.dart';



class FlutterNativeLog {

static const MethodChannel _channel =

const MethodChannel('flutter_native_log');



static Future<String> get platformVersion async {

final String version = await _channel.invokeMethod('getPlatformVersion');

return version;

}

}

This will be your main dart for interacting with the native code

Inside your android src folder you will find skotlin file

package io.github.ponnamkarthik.flutternativelog



import io.flutter.plugin.common.MethodChannel

import io.flutter.plugin.common.MethodChannel.MethodCallHandler

import io.flutter.plugin.common.MethodChannel.Result

import io.flutter.plugin.common.MethodCall

import io.flutter.plugin.common.PluginRegistry.Registrar



class FlutterNativeLogPlugin(): MethodCallHandler {

companion object {

@JvmStatic

fun registerWith(registrar: Registrar): Unit {

val channel = MethodChannel(registrar.messenger(), "flutter_native_log")

channel.setMethodCallHandler(FlutterNativeLogPlugin())

}

}



override fun onMethodCall(call: MethodCall, result: Result): Unit {

if (call.method.equals("getPlatformVersion")) {

result.success("Android ${android.os.Build.VERSION.RELEASE}")

} else {

result.notImplemented()

}

}

}

Inside your iOS directory you will find FlutterNativeLog.m file

#import "FlutterNativeLogPlugin.h"



@implementation FlutterNativeLogPlugin

+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {

FlutterMethodChannel* channel = [FlutterMethodChannel

methodChannelWithName:@"flutter_native_log"

binaryMessenger:[registrar messenger]];

FlutterNativeLogPlugin* instance = [[FlutterNativeLogPlugin alloc] init];

[registrar addMethodCallDelegate:instance channel:channel];

}



- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {

if ([@"getPlatformVersion" isEqualToString:call.method]) {

result([@"iOS " stringByAppendingString:[[UIDevice currentDevice] systemVersion]]);

} else {

result(FlutterMethodNotImplemented);

}

}



@end

This is a sample plugin which will interact with your native device and revert back the version of android/iOS you are on.