mintware-de / flutter_barcode_reader

A flutter plugin for reading 2D barcodes and QR codes.
MIT License
628 stars 463 forks source link

Adding A Plugin To MainActivity "Deregisters" barcode_scan Plugin #203

Closed ibcoleman closed 4 years ago

ibcoleman commented 4 years ago

Great plugin; worked flawlessly out of the box. The issue I'm having is that when I add any MethodChannel to my MainActivity in Android, barcode_scan "disappears". The error message is

E/flutter ( 8950): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: MissingPluginException(No implementation found for method scan on channel de.mintware.barcode_scan)
E/flutter ( 8950): #0      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:154:7)

The MainActivity is as simple as I could make it:

package com.example.flutter_prototype

import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel

class MainActivity: FlutterActivity() {
    companion object {
        const val CHANNEL = "arbitrarystring.tacticaledge.us.channel"
    }

    override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
        MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler { call, result ->
            if (call.method == "getStringMap") {
                try {
                    //String arg = call.
                    val resultMap = mutableMapOf<String, String>()
                    resultMap["aString"] = "Here\'s a String From Android!!";
                    result.success(resultMap)

                } catch (t : Throwable) {
                    result.error("ERROR", t.message, "")

                }
            }
        }
    }
}

I'm assuming there's a very good chance I'm screwing something up.

ibcoleman commented 4 years ago

Turns out I needed to call this registration static method to make sure "generated" plugins were registered when adding the new MethodChannel...

    override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
        **GeneratedPluginRegistrant.registerWith(flutterEngine)**
        ...
    }