Flutter plugin native iOS func handle not being called
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)
}
}
}
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, 2022Comments
-
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 thisimport 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 almost 3 yearsThanks for answering. I moved it inside class but it still doesn't work
-
happy_san almost 3 yearsDid you use debugger to check if
handle
is being called now? -
iRohitBhatia almost 3 yearsI did. it's not being called.
-
happy_san almost 3 yearsAt this point, I can recommend double-checking if you're calling the correct methods and such. Is your full code hosted publically?
-
iRohitBhatia almost 3 yearsHaha. Stupid me. I actually tried what you said but forgot to call that method from dart side. Thanks for looking into it bro :)