Flutter 插件本机 iOS func 句柄未被调用

Flutter plugin native iOS func handle not being called

所以,我正在 iOS 开发我的第一个 flutter 插件,但我无法使本地方法工作。

例如,从样板代码中,我有

import 'dart:async';

import 'package:flutter/services.dart';

class MobileFlutter {
  static const MethodChannel _channel =
      const MethodChannel('xyz_client');

// Asking this channel to invoke method get platform version
  static Future<String?> get platformVersion async {
    final String version = await _channel.invokeMethod('getPlatformVersion');
    return version+"d";
  }
}

然后在 iOS

SwiftMobileFlutterPlugin.swift 我有这个

import Flutter
import UIKit

public class SwiftMobileFlutterPlugin: NSObject, FlutterPlugin {
  public static func register(with registrar: FlutterPluginRegistrar) {
    let channel = FlutterMethodChannel(name: "xyz_client", binaryMessenger: registrar.messenger())
    let instance = SwiftMobileFlutterPlugin()
    registrar.addMethodCallDelegate(instance, channel: channel)
  }
  public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
    if ("getPlatformVersion" == call.method) {
        result("iOS " + UIDevice.current.systemVersion)
    } 
}

我用了调试器,但从来没有到过这个地步。

public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult)

Ps: public static func register(with registrar: FlutterPluginRegistrar) { 正在呼叫中。

谁能帮我调试一下?

参考this示例代码,我想不同的是handle函数应该在classSwiftMobileFlutterPlugin中。喜欢:

import Flutter
import UIKit

public class SwiftMobileFlutterPlugin: NSObject, FlutterPlugin {
  public static func register(with registrar: FlutterPluginRegistrar) {
    let channel = FlutterMethodChannel(name: "xyz_client", binaryMessenger: registrar.messenger())
    let instance = SwiftMobileFlutterPlugin()
    registrar.addMethodCallDelegate(instance, channel: channel)
  }

  public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
    if ("getPlatformVersion" == call.method) {
        result("iOS " + UIDevice.current.systemVersion)
    } 
 }
}