【问题标题】:Unhandled Exception: MissingPluginException(No implementation found for method flutterToWatch on channel it.example.watch)未处理的异常:MissingPluginException(在通道 it.example.watch 上找不到方法 flutterToWatch 的实现)
【发布时间】:2022-07-24 19:45:34
【问题描述】:

我正在使用awesome_notifications 库,例如,为了防止 MissingPluginException(No implementation found for method getAll on channel plugins.flutter.io/shared_preferences) 它在 AppDelegate 中使用: p>

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")!)
      }

但我的自定义频道仍有错误: 未处理的异常: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)
                }
            })
        }
    }

【问题讨论】:

    标签: flutter


    【解决方案1】:

    我引用https://github.com/flutter/flutter/issues/98473 为我解决了这个问题:

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

    对于 Flutter 2.11+(目前是 master),调用 DartPluginRegistrant.ensureInitialized(),它将为所有需要它的插件运行 Dart 插件注册。 对于旧版本的 Flutter,手动调用您使用的任何具有 Dart 注册的插件实现的 registerWith。对于 shared_preferences,详见上文。

    由于未来的计划是要求 DartPluginRegistrant.ensureInitialized() 在隔离中,按固定方式关闭。

    【讨论】:

    • 我尝试了 DartPluginRegistrant.ensureInitialized(),但没有任何变化,仍然出现错误。 DartPluginRegistrant 工作存在问题github.com/flutter/flutter/issues/99883
    • 你的颤振版本是什么?
    • 2.12.0-4.1.pre 版本
    【解决方案2】:

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

    GeneratedPluginRegistrant.register(with: self)
    

    我解决了这个问题

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-20
      • 2020-11-13
      • 1970-01-01
      • 1970-01-01
      • 2022-07-22
      • 2021-07-10
      • 2020-05-29
      • 1970-01-01
      相关资源
      最近更新 更多