flutter / flutter

Flutter makes it easy and fast to build beautiful apps for mobile and beyond
https://flutter.dev
BSD 3-Clause "New" or "Revised" License
166.29k stars 27.52k forks source link

MissingPluginException(No implementation found for method listen on channel xxx) #123584

Closed KalaliEhsan closed 1 year ago

KalaliEhsan commented 1 year ago

I use native code in my flutter app by EventChannel like the following code.

class MainActivity : FlutterActivity() {
    private val ACTION_BROADCAST_RECEIVER = "com.datalogic.decodewedge.decode_action"
    private val CATEGORY_BROADCAST_RECEIVER = "com.datalogic.decodewedge.decode_category"
    private val EXTRA_DATA_STRING = "com.datalogic.decode.intentwedge.barcode_string"
    private val EVENT_CHANNEL_BARCODE_INTENT = "app.channel.event.barcode_intent"
    private var receiver: BroadcastReceiver? = null
    private var filter: IntentFilter? = null

    override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
        GeneratedPluginRegistrant.registerWith(flutterEngine)
        EventChannel(
            flutterEngine.dartExecutor.binaryMessenger,
            EVENT_CHANNEL_BARCODE_INTENT
        ).setStreamHandler(
            object : EventChannel.StreamHandler {
                override fun onListen(args: Any?, events: EventSink?) {
                    receiver = barcodeIntentReceiver(events)
                    filter = IntentFilter()
                    filter!!.addAction(ACTION_BROADCAST_RECEIVER)
                    filter!!.addCategory(CATEGORY_BROADCAST_RECEIVER)
                    registerReceiver(receiver, filter)
                }

                override fun onCancel(args: Any) {
                    Log.i("", "EVENT_CHANNEL_BARCODE_INTENT has been canceled")
                }
            }
        )
    }

    private fun barcodeIntentReceiver(events: EventSink?): BroadcastReceiver? {
        return object : BroadcastReceiver() {
            override fun onReceive(p0: Context?, wedgeIntent: Intent?) {
                val action = wedgeIntent!!.action
                if (action == ACTION_BROADCAST_RECEIVER) {
                    val barcode_text = wedgeIntent.getStringExtra(EXTRA_DATA_STRING)
                    events?.success(barcode_text)
                }
            }
        }
    }
}

When I run the app, an error occurs like the following:

======== Exception caught by services library ======================================================
The following MissingPluginException was thrown while activating platform stream on channel app.channel.event.barcode_intent:
MissingPluginException(No implementation found for method listen on channel app.channel.event.barcode_intent)

When the exception was thrown, this was the stack: 
#0      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:313:7)
<asynchronous suspension>
#1      EventChannel.receiveBroadcastStream.<anonymous closure> (package:flutter/src/services/platform_channel.dart:662:9)
<asynchronous suspension>
====================================================================================================

This way didn't work. How do I resolve it?

exaby73 commented 1 year ago

Hi @KalaliEhsan This issue doesn't seem to describe a bug or a feature request. Please see https://flutter.dev/community for resources and asking questions like this, you may also get some help if you post it on Stack Overflow and if you need help with your code, please see https://www.reddit.com/r/flutterhelp/ Closing, as this isn't an issue with Flutter itself. If you disagree, please write in the comments and I will reopen it. Thank you

KalaliEhsan commented 1 year ago

Thanks a lot, @exaby73

github-actions[bot] commented 1 year ago

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.