Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To use Swift's FlutterMethodChannel without flutterViewController, you can follow these steps:

  1. Create a new FlutterEngine instance:
let engine = FlutterEngine(name: "my_engine")
engine.run();
  1. Instantiate the FlutterMethodChannel:
guard let flutterChannel = FlutterMethodChannel(name: "my_channel", binaryMessenger: engine.binaryMessenger) else {
    return
}
  1. Set up the method to be called from Flutter:
func myMethod(arguments: Any?) -> String? {
    // Code to execute
    return "Result of my method"
}
  1. Set up the call handler to receive calls from Flutter:
flutterChannel.setMethodCallHandler { [weak self] (call: FlutterMethodCall, result: FlutterResult) in
    guard call.method == "myMethod" else {
        result(FlutterMethodNotImplemented)
        return
    }
    let arguments = call.arguments
    let response = self?.myMethod(arguments: arguments)
    result(response)
}

With these steps, you should be able to use Swift's FlutterMethodChannel without the need for flutterViewController.