未处理的异常:MissingPluginException(在通道 it.example.watch 上找不到方法 flutterToWatch 的实现)

Ars*_*aev 26 flutter

我正在使用Awesome_notifications库,并防止它在 AppDelegate 中使用,例如MissingPluginException(在通道 plugins.flutter.io/shared_preferences 上找不到方法 getAll 的实现) :

SwiftAwesomeNotificationsPlugin.setPluginRegistrantCallback { registry in
          SwiftAwesomeNotificationsPlugin.register(
            with: registry.registrar(forPlugin: "io.flutter.plugins.awesomenotifications.AwesomeNotificationsPlugin")!)          
          FLTSharedPreferencesPlugin.register(
            with: registry.registrar(forPlugin: "io.flutter.plugins.sharedpreferences.SharedPreferencesPlugin")!)
      }
Run Code Online (Sandbox Code Playgroud)

但我的自定义通道仍然有错误: 未处理的异常:MissingPluginException(在通道 it.example.watch 上找不到方法 flutterToWatch 的实现) 而且我不知道如何像 Awesome_notifications 那样在后台注册它。

我的频道:

private func initFlutterChannel() {
        if let controller = window?.rootViewController as? FlutterViewController {
            let channel = FlutterMethodChannel(
                name: "it.example.watch",
                binaryMessenger: controller.binaryMessenger)
            
            channel.setMethodCallHandler({ [weak self] (
                call: FlutterMethodCall,
                result: @escaping FlutterResult) -> Void in
                switch call.method {
                case "flutterToWatch":
                    guard let watchSession = self?.session, watchSession.isPaired, watchSession.isReachable, let methodData = call.arguments as? [String: Any], let method = methodData["method"], let data = methodData["data"] else {
                                            result(false)
                                            return
                                        }
                    
                    let watchData: [String: Any] = ["method": method, "data": data]
                    
                    // Pass the receiving message to Apple Watch
                    watchSession.sendMessage(watchData, replyHandler: nil, errorHandler: nil)
                    result(true)
                default:
                    result(FlutterMethodNotImplemented)
                }
            })
        }
    }
Run Code Online (Sandbox Code Playgroud)

小智 48

只需关闭编辑器上运行的应用程序,然后再次启动您的应用程序运行(不要热重启/热重加载),停止您的应用程序并再次启动它!


Tur*_*rvy 13

我最近在更改几个软件包时遇到了这个错误。

我只是用 清除 Flutter flutter clean,删除 pubspec.lock 和Invalidate and RestartAndroid Studio。最后flutter pub get恢复包裹,一切正常。


Vep*_*row 5

我在这里遇到了同样的问题,错误出现在 AppDelegate.swift 中,通过输入这行代码

GeneratedPluginRegistrant.register(with: self)
Run Code Online (Sandbox Code Playgroud)

我解决了这个问题


Ole*_*Ole 3

我引用https://github.com/flutter/flutter/issues/98473来解决我的问题:

总结实际的解决方案:在需要使用插件的任何隔离的入口点开始(您可能已经在调用 WidgetsFlutterBinding.ensureInitialized()):

对于 Flutter 2.11+(当前为 master),调用 DartPluginRegistrant.ensureInitialized(),这将为所有需要它的插件运行 Dart 插件注册。对于旧版本的 Flutter,请手动调用您使用的已注册 Dart 的任何插件实现的 registerWith。对于shared_preferences,请参阅上面的详细信息。

由于未来的计划是要求DartPluginRegistrant.ensureInitialized()隔离,因此关闭是固定的。