Vivek Yadav
I am Google Developer Exper for Flutter. Also, I am tech lead at Tata Digital.
How to build best plugins in Flutter
Lead Mobile Developer @ ZestMoney
Google Developer Expert for Flutter and Dart
Community leader @FlutterMumbai, @IndiaFlutter
Teacher, Mentor, Trainer, and Contributor for Flutter in every possible way
@viveky259259
“Flutter is the best platform to build apps for iPhones and Android phones.”
Designed for the Future
A specialized Dart package that contains an API written in Dart code combined with one or more platform-specific implementations.
Packages enable the creation of modular code that can be shared easily.
Flutter Side
Platform Side
flutter create --template=plugin --platforms=android,ios,macos,web,windows,linux flutter_security
1. Create plugin
// Kotlin: FlutterSecurityPlugin.kt
override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {
when (call.method){
"encrypt" -> print("do encryption")
"decrypt" -> print("do decryption")
else -> {
print("Not implemented")
}
}
}
2. Setup Communication
// Flutter: flutter_security_platform_interface
Future<String> encrypt() async {
throw UnimplementedError('encrypt() has not been implemented.');
}
Future<String> decrypt() async {
throw UnimplementedError('decrypt() has not been implemented.');
}
2. Setup Communication
//flutter_security_method_channel.dart
@override
Future<String> decrypt() async {
final String? decryptedString =
await methodChannel.invokeMethod<String>('decrypt');
if (decryptedString != null) {
return decryptedString;
} else {
throw Exception();
}
}
2. Setup Communication
//flutter_security_method_channel.dart
@override
Future<String> encrypt() async {
final String? encryptedString =
await methodChannel.invokeMethod<String>('encrypt');
if (encryptedString != null) {
return encryptedString;
} else {
throw Exception();
}
}
// flutter_securiy.dart
class FlutterSecurity {
Future<String> encrypt() async {
return FlutterSecurityPlatform.instance.encrypt();
}
Future<String> decrypt() async {
return FlutterSecurityPlatform.instance.decrypt();
}
}
2. Setup Communication
// main.dart
class ExampleApp{
final String _flutterSecurityPlugin = FlutterSecurity();
_flutterSecurityPlugin.encrypt(plainText);
_flutterSecurityPlugin.decrypt(cipherText);
}
3. Use in Flutter App
// Encryption.kt
package com.example.flutter_security
class Encryption {
fun encrypt(plainText: String): String {
return Base64.getEncoder().encodeToString(cipherText)
}
fun decrypt(cipherText:String):String{
return cipher.doFinal(Base64.getDecoder().decode(cipherText))
}
}
4. Add platform implementation
How to become successful?
Success is doing what you love!
Twitter: @viveky259259
Github: https://github.com/viveky259259
Instagram: @Viveky259
Medium : @Viveky259259
Vivek Flutter
By Vivek Yadav
How to convert your existing native apps to Flutter? Twitter: twitter.com/viveky252959
I am Google Developer Exper for Flutter. Also, I am tech lead at Tata Digital.