I'm trying to develop a simple plugin that invokes a method and prints the string returned from the respective native platform like so:
ProximityPlugin.dart
class ProximityPlugin {
static const MethodChannel methodChannel = const MethodChannel('methodChannel');
Future<String?> createProximityObserver(String appID, String appToken) async {
try {
// Invoke the platform-specific method
var result = await methodChannel.invokeMethod('create');
return result;
} on PlatformException catch (e) {
print(e.message);
return e.message;
}
}
}
AppDelegate.swift
import UIKit
import Flutter
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
@nonobjc override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
GeneratedPluginRegistrant.register(with: self)
let controller : FlutterViewController = window?.rootViewController as! FlutterViewController
let channel = FlutterMethodChannel(name: "methodChannel", binaryMessenger: controller.binaryMessenger)
channel.setMethodCallHandler { [weak self] (call: FlutterMethodCall, result: @escaping FlutterResult) -> Void in
guard call.method == "create" else {
result(FlutterError(code:"ERROR", message: "createProximityObserver not implemented", details: nil))
return
}
result(nil)
}
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}
My problem is, I keep facing this error: Unhandled Exception: MissingPluginException(No implementation found for method create on channel methodChannel)
I have tried rebooting, flutter clean, flutter pub get, relaunching Xcode, closing the app, removing the app, just about anything I could find online. I even tried copying and pasting the example in the Flutter documentation line for line, and ran into the same error (except obviously it complained about different method and channels).
I'm starting to think I missed a step somewhere, any help is greatly appreciated.
I was expecting something back other than an error since I created the methodChannel in the AppDelegate, or at least I think I did correctly.