Write Flutter plugin

Quan Ngo
2 min readApr 1, 2021

Create flutter plugin

Android side

add lib needed in android/build.gradle

dependencies {
...
implementation 'foundation.icon:icon-sdk:1.0.0'
...
}

To remove code error: right click at android folder -> Flutter -> Open Android Module …..

Then start to write code in the onMethodCall function on *.kt plugin file

The result return should be in String so we can decode it to json in dart side later

result.success("{\"txHash\":\"${txHash}\",\"status\":0}")

Ios side

Right click at example/ios -> Flutter -> Open Ios module ….

Then we can write code with full code analyze and suggestion in the Pods folder

To add library in Ios side of plugin, add in .podspec file, then need to pod install the example again to remove the error happen

s.dependency 'ICONKit', '0.3.1'

If the error still happen when trying to access the library methods, try to create new file and set target to the library.

the return should also be string

result("{\"balance\":\"\(balance)\"}")

can use this website to convert json to string https://www.functions-online.com/json_encode.html

That’s all needed to create a Flutter plugin

To test the plugin, we cannot use unit test as we need to call method channels on Android and Ios. So we can implement integration test using https://pub.dev/packages/integration_test

the example test found here

sample code for the plugin i wrote

https://github.com/quango2304/flutter_icon_network

--

--