I try to return a string from Swift's side of my Flutter plugin to Dart's:
public class SwiftAMPlugin: NSObject, FlutterPlugin {
public static func register(with registrar: FlutterPluginRegistrar) {
let channel = FlutterMethodChannel(name: "aM", binaryMessenger: registrar.messenger())
let instance = SwiftAMPlugin()
registrar.addMethodCallDelegate(instance, channel: channel)
}
private func getStr(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
result("test");
}
public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
switch (call.method) {
case "getStr":
getStr(call, result: result);
break;
default:
result(false);
}
}
}
On Dart's side:
static Future<String> getStr() async {
try {
return await _channel.invokeListMethod('getStr');
} catch(e, s) {
print(e);
print(s);
}
}
But it crashes on getStr call with error:
2020-05-24 00:05:17.547324+0300 Runner[4146:2064989] flutter: MissingPluginException(No implementation found for method getStr on channel aM)
2020-05-24 00:05:17.552161+0300 Runner[4146:2064989] flutter: #0 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:154:7)
I have a similar implementation for Android and it works perfectly.
Where can the problem be?
P.S. Imma noobie in Flutter, maybe do I need to describe my native method implementation somewhere? Now I just have switch block to find required method.