Ask Your Question
0

How to use swift's flutterMethodChannel without flutterViewController?

asked 2023-07-07 12:53:03 +0000

djk gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2023-07-07 13:06:01 +0000

plato gravatar image

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-07-07 12:53:03 +0000

Seen: 11 times

Last updated: Jul 07 '23