Flutter plugin native iOS func handle not being called

Viewed 637

So, I am developing my first flutter plugin in iOS but I am unable to make native method to work.

For example, from the boiler plate code, I have

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";
  }
}

and then in iOS

SwiftMobileFlutterPlugin.swift I have this

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

I used debugger but it never comes to this point.

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

Ps: public static func register(with registrar: FlutterPluginRegistrar) { is being called.

Can someone help me in debugging?

1 Answers

Consulting this sample code, I suppose the difference is that the handle function is meant to be in the class SwiftMobileFlutterPlugin. Like:

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)
    } 
 }
}
Related