androidfluttererror-handlingflutter-method-channelflutter-add-to-app

ERROR:flutter/runtime/dart_vm_initializer.cc(41) Unhandled Exception:MissingPluginException(No implementation found for method encryption on channel)


I'm using add to app flutter module in Android. When i try to use methodchannel to use method from Android i'm always get error such like this

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: MissingPluginException(No implementation found for method encryption on channel) #0 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:308:7) #1 _WhitePageState._encryption (package:demoflutter/ui/pages/white_page.dart:33:21)

This is my Flutter Code :

var channelName = const MethodChannel("com.bca.msb/encryption");

  final emailController = TextEditingController(text: '');
  final cardnumberController = TextEditingController(text: '');
         Future<void> _encryption() async{
          String encryptedCN;
          String encryptedEM;
            try{
          final String cM = await channelName.invokeMethod("encryption", {'cardNumber' : cardnumberController.text});
          encryptedCN = cM;
        
          final String eM = await channelName.invokeMethod("encryption", 
      {'email': emailController.text});
          encryptedEM = eM;
          print("$encryptedCN $encryptedEM");
        }on PlatformException catch(e){
        
            encryptedCN = 'Failed CN ${e.message}';
            encryptedEM = 'Failed Em ${e.message}';
        }
        
            setState(() {
              cardnumberController.text = encryptedCN;
              emailController.text = encryptedEM;
            });
          }

This is my Android Code :

  class Encryption: FlutterActivity() {
        private val channelName = "com.bca.msb/encryption";
    
        override fun configureFlutterEngine(flutterEngine: FlutterEngine) 
       {
            super.configureFlutterEngine(flutterEngine)
    
            MethodChannel(flutterEngine.dartExecutor.binaryMessenger,channelName).setMethodCallHandler { call, result ->
                var args = call.arguments as Map<String, String>
                var CN = args["cardNumber"]
                var EM = args["email"]
                if (call.method == "encryption"){
                    if (args.containsKey("cardNumber")){
                        val encryptCN = CN?.let { encryption(it) }
    
                        result.success(encryptCN)
                    }else if (args.containsKey("email")) {
                        val encryptEM = EM?.let{encryption(it)}
                        result.success(encryptEM)
                    }
                }else {
                    result.notImplemented()
                }
    
            }
        }
    
        private fun encryption(str : String): String {
            var cM = ""
    
            cM = str.encrypt().toString()
       
            return cM
        }
    }

How i can solve this error?


Solution

  • So I have found the solution of my problem,

    When you want to invoke a method or want to use a method in Existing Android (note: Not the Android package found in Flutter but an existing Android application). You have to equate the engines that will be used by Android and Flutter. The solution to my case is as follows.

    btnRegisterNow.setOnClickListener {
            startActivity(
                context?.let { it1 ->
                        FlutterActivity
                            .NewEngineIntentBuilder(Encryption::class.java)
                            .initialRoute("/")
                            .build(it1)
                }
            )
        }
    

    The code above will create the same engine when you first want to switch from an Android page to Flutter. The code above will create the same engine when you first want to switch from an Android page to Flutter. The code above will build the same engine from the Encryption class, where this class is used to process the methodchannel resulting from data sent from Flutter, and after that the MethodChannel will be used successfully and show the expected results. MethodChannel is proven to work in applications that implement add-to-apps. And dont forget to register Encryption activity in your Manifest like this

     <activity
        android:name=".base.Encryption"
        android:windowSoftInputMode="adjustResize"
        android:exported="false">
      <intent-filter>
        <action android:name="com.example.msb.base.ACTION"/>
        <category android:name="android.intent.category.DEFAULT"/>
      </intent-filter>
    </activity>
    

    That's All, Thanks