Flutter plugin native iOS func handle not being called

495

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)
    } 
 }
}
Share:
495
iRohitBhatia
Author by

iRohitBhatia

Rohit is a full-stack Developer and an open-source contributor. He have experience working as lead developer at 3 YC and 1 Sequoia backed startup. You can strike a conversation with him about anything, especially but not limiting to startups, product ideas, technology, or challenge him for a game of chess. Rohit has always been passionate about cool workplaces, and in the past, he had his venture - Spaceyfi, a Coworking space aggregator and world’s first platform where you can find and work with like-minded people.

Updated on December 30, 2022

Comments

  • iRohitBhatia
    iRohitBhatia over 1 year

    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?

  • iRohitBhatia
    iRohitBhatia almost 3 years
    Thanks for answering. I moved it inside class but it still doesn't work
  • happy_san
    happy_san almost 3 years
    Did you use debugger to check if handle is being called now?
  • iRohitBhatia
    iRohitBhatia almost 3 years
    I did. it's not being called.
  • happy_san
    happy_san almost 3 years
    At this point, I can recommend double-checking if you're calling the correct methods and such. Is your full code hosted publically?
  • iRohitBhatia
    iRohitBhatia almost 3 years
    Haha. Stupid me. I actually tried what you said but forgot to call that method from dart side. Thanks for looking into it bro :)