Vivek yadav

plugins in flutter

How to build best plugins in Flutter

Agenda

Vivek yadav

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.”

Vivek Yadav

Google Developer Expert for Flutter & Dart

@viveky259259

Designed for the Future

Let's begin

What is flutter?

  • Toolkit to build beautiful UIs
  • Framework to build faster apps
  • Framework to build for multiple platforms
  • Single codebase, code once

What is a Flutter Plugin

A specialized Dart package that contains an API written in Dart code combined with one or more platform-specific implementations.

What is a Flutter Package

Packages enable the creation of modular code that can be shared easily.

Understand Flutter Package

Understand Flutter Plugin

Developing packages 

  • All Dart code
  • Classes, widgets, and functions

Developing Plugin 

Flutter Side

  • All Dart codes
  • Classes, widgets, and functions
  • Communication

Platform Side

  • All Platform codes (Kotlin, swift etc)
  • Classes, UI elements, and functions
  • Communication

Implementation

Plugin implementation

flutter create --template=plugin --platforms=android,ios,macos,web,windows,linux flutter_security

1. Create plugin

Plugin implementation

// 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

Plugin implementation

// 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

Plugin implementation

//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();
    }
  }

Plugin implementation

// flutter_securiy.dart

class FlutterSecurity {
  Future<String> encrypt() async {
    return FlutterSecurityPlatform.instance.encrypt();
  }

  Future<String> decrypt() async {
    return FlutterSecurityPlatform.instance.decrypt();
  }
}

2. Setup Communication

Plugin implementation

// main.dart

class ExampleApp{

 final String _flutterSecurityPlugin = FlutterSecurity();
 
 _flutterSecurityPlugin.encrypt(plainText);
 
 _flutterSecurityPlugin.decrypt(cipherText);
 
}

3. Use in Flutter App

Plugin implementation

// 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

Questions

&

ANSWERS

How to become successful?

Success is doing what you love!

Vivek Yadav

Google Developer Expert for Flutter & Dart

@viveky259259

Thank you!

Vivek Flutter

Join Moreficent!

Build package and plugin-session

By Vivek Yadav

Build package and plugin-session

How to convert your existing native apps to Flutter? Twitter: twitter.com/viveky252959

  • 200