Unhandled Exception: MissingPluginException(No implementation found for method flutterToWatch on channel it.example.watch)

Viewed 5718

I am using awesome_notifications library, and to prevent, for example, MissingPluginException(No implementation found for method getAll on channel plugins.flutter.io/shared_preferences) it uses in AppDelegate:

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

But I still have error with my custom channel: Unhandled Exception: MissingPluginException(No implementation found for method flutterToWatch on channel it.example.watch) And I don't know how to register it in the background like awesome_notifications do it.

My channel:

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)
                }
            })
        }
    }
3 Answers

I quote from https://github.com/flutter/flutter/issues/98473 which solved the problem for me:

To summarize the actual solution: at the start of the entry point for any isolate that needs to use plugins (where you are presumably already calling WidgetsFlutterBinding.ensureInitialized()):

For Flutter 2.11+ (currently master), call DartPluginRegistrant.ensureInitialized(), which will run the Dart plugin registration for all plugins that need it. For older versions of Flutter, manually call the registerWith of any plugin implementations you use that have Dart registration. For shared_preferences, see above for details.

Since the plan going forward is to require DartPluginRegistrant.ensureInitialized() in isolates, closing as fixed.

I've faces same issue here and error was in AppDelegate.swift By putting in this line of code

GeneratedPluginRegistrant.register(with: self)

I solved this problem

I also faced the error-

Error: MissingPluginException(No implementation found for method initialize on channel plugin.csdcorp.com/speech_to_text)

This page- Github issue link helped me. I changed my plugin version in pubspec.yaml file and it worked for me.

Edited code

Or

you can use the command in terminal-

flutter pub outdated
flutter pub upgrade --major-versions
Related