编辑3-4-2019

经过更多研究后,我发现当使用flutter create --org com.example --template=plugin hello命令生成新插件时,它仍然使用旧的pubspec声明格式。但是,如果我们将旧格式更改为新格式,则会产生与我遇到的相同的错误。

原始帖子

I'm working on a Flutter plugin,在将插件迁移到Flutter v2嵌入之前,我在pubspec中有以下声明:

flutter:
  plugin:
    packageAndroid: dev.steenbakker.flutter_ble_peripheral
    pluginClass: FlutterBlePeripheralPlugin

但是,在the guide provided by flutter中,建议使用新的声明类型,该声明将不同的平台分开,如下所示:
flutter:
  plugin:
    platform:
      ios:
        pluginClass: FlutterBlePeripheralPlugin
      android:
        package: dev.steenbakker.flutter_ble_peripheral
        pluginClass: FlutterBlePeripheralPlugin

但是,这样做之后,每次我想从库中执行某些操作时,都会出现以下错误:
2020-04-01 13:54:03.134943+0200 Runner[1328:1208976] [VERBOSE-2:ui_dart_state.cc(157)] Unhandled Exception: MissingPluginException(No implementation found for method start on channel dev.steenbakker.flutter_ble_peripheral/ble_state)
#0      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:319:7)
<asynchronous suspension>
#1      FlutterBlePeripheral.start (package:flutter_ble_peripheral/flutter_ble_peripheral.dart:48:26)
#2      _MyAppState.startBroadcast (package:flutter_ble_peripheral_example/main.dart:53:19)
#3      _MyAppState.build.<anonymous closure> (package:flutter_ble_peripheral_example/main.dart:65:32)

我已经在android上完全实现了新的嵌入,如下所示:
/** FlutterBlePeripheralPlugin */
class FlutterBlePeripheralPlugin: FlutterPlugin, MethodCallHandler, EventChannel.StreamHandler {

  private var applicationContext: Context? = null
  private var methodChannel: MethodChannel? = null
  private var eventChannel: EventChannel? = null
  private var peripheral: Peripheral? = null
  private var eventSink: EventChannel.EventSink? = null
  private var advertiseCallback: (Boolean) -> Unit = { isAdvertising ->
    eventSink?.success(isAdvertising)
  }

/** Plugin registration embedding v1 */
  companion object {
    @JvmStatic
    fun registerWith(registrar: PluginRegistry.Registrar) {
     FlutterBlePeripheralPlugin().onAttachedToEngine(registrar.context(), registrar.messenger())
    }
  }

/** Plugin registration embedding v2 */
  override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
  onAttachedToEngine(flutterPluginBinding.applicationContext, flutterPluginBinding.binaryMessenger)
  methodChannel!!.setMethodCallHandler(this)
  eventChannel!!.setStreamHandler(this)
  peripheral = Peripheral()
  peripheral!!.init(flutterPluginBinding.applicationContext)
}

  private fun onAttachedToEngine(applicationContext: Context, messenger: BinaryMessenger) {
    this.applicationContext = applicationContext
    methodChannel = MethodChannel(messenger, "dev.steenbakker.flutter_ble_peripheral/ble_state")
    eventChannel = EventChannel(messenger, "dev.steenbakker.flutter_ble_peripheral/ble_event")
    methodChannel!!.setMethodCallHandler(this)
    eventChannel!!.setStreamHandler(this)
    peripheral = Peripheral()
    peripheral!!.init(applicationContext)
  }

使用旧的pubspec时,iOS和Android都可以正常运行,但是,新的pubspec样式在iOS和Android上都给出相同的错误

最佳答案

您的pubspec.yaml中有一个错字。正确的关键字是“平台”,而不是“平台”。所以这应该解决它:

flutter:
  plugin:
    platforms:
      ios:
        pluginClass: FlutterBlePeripheralPlugin
      android:
        package: dev.steenbakker.flutter_ble_peripheral
        pluginClass: FlutterBlePeripheralPlugin

关于android - 新的pubspec Flutter插件声明导致MissingPluginException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60971114/

10-09 01:03