There’s an iOS and Android folder within the flutter workspace, by default it’s on the identical degree because the lib folder, we are going to use these to jot down platform-specific code.
Open the iOS module in Xcode, and observe these steps:
- Find your challenge in finder.
- Go to the ios folder, in case your challenge identify is
simple_platform_channel > ios.
- Within the iOS folder open
Be aware for non-iOS devs:
Runner.xcodeproj is the challenge file, however as a Flutter challenge template by default makes use of Cocopods as a dependency supervisor it creates a workspace file that embeds dependencies.
Let’s write the native technique, however had been to jot down it?
Right here is the trail:
Runner > Runner > AppDelegate
You will notice some code is already there. We are going to perceive it earlier than making any modifications.
AppDelegate class that’s annotated with
@UIApplicationMain, so principally from this execution of this system begins, it is equal to void
primary() in Flutter.
Now if we see inside the category there’s one technique
didFinishLaunchingWithOptions because the identify signifies this technique is executed as quickly because the app launches. We are going to make modifications to this technique.
We are going to now use the PlatformChannel that we now have outlined within the Flutter.
Right here is the code. Don’t fear if it seems to be scary; we are going to go over it step-by-step:
Let’s perceive the above snippet:
- We try to get the
FlutterViewController, in case you might be questioning the place did window come into the scene, it is principally outlined in
FlutterAppDelegatewhich is prolonged by the
FlutterViewControlleris ready as a
rootViewControllerso, we try to entry it.
- Why are we creating a technique channel once more?
As we try to have an interplay between two frameworks the connections needs to be established on either side.
– In an effort to set up a connection, we have to have the identical channel identify as we now have outlined within the Flutter.
binaryMessenger. We’re utilizing the default binary messenger supplied by
FlutterViewController. Because of this we pulled out the
- Right here we’re getting ready the tactic handler. It would deal with any technique name invoked on the tactic channel.
- We’re attaching a technique name again handler, and passing a closure (in case you might be questioning what’s that, it’s simply an nameless operate. When you’ve used
Listing.forEachin flutter, then you realize it already).
There are two parameters to the nameless operate:
– name ( FlutterMethodCall) — It offers the small print of the tactic name, like technique identify and its arguments.
– end result (FlutterResult) — That is the end result you’ll return again to the flutter module, it’s denoted because the @escaping which implies it’s an async name, so you’ll be able to carry out some time-taking operation after which name the end result.
- Simply checking if the invoked technique identify is
getDeviceModel. Right here it doesn’t make a lot sense to verify, however when there are a number of strategies you have to verify to differentiate between them.
- Calling one other technique to get the system Mannequin.
- We’ve got used the iOS
systemAPI to get the end result.
- Calling the
end resultwith the information, which helps in passing this again to the Flutter module.
- If we invoke some technique from the Flutter aspect which isn’t carried out in native we throw
Easy isn’t it!
Now let’s have a look at some necessary questions across the platform channel.
move some knowledge from Flutter to iOS, whereas invoking a technique?
Bear in mind, we mentioned the invokeMethod which takes in an array of dynamic args. Let’s see an instance:
"flutterAppVersion": "0.0.1", "developerName": "XYZ");
move knowledge again to the Flutter module?
We will move any knowledge within the end result. For instance:
end result( ------ )
However so as to simplify issues a bit, Flutter present different options to the
1. Future<T?> invokeMethod<T>(String technique, [ dynamic arguments ])
2. Future<Map<Ok, V>?> invokeMapMethod<Ok, V>(String technique, [ dynamic arguments ])
3. Future<Listing<T>?> invokeListMethod<T>(String technique, [ dynamic arguments ])
Although the parameter handed to this technique are the identical, the return varieties are totally different, so we are able to use
invokeMapMethod if we wish a map again within the end result and so forth.
In 5 easy steps, you’ll be able to run platform-specific code in Flutter:
- Create a Methodology channel in Flutter.
- Invoke a technique on the channel and await the end result.
- Create a FlutterMethodChannel in iOS, within the didFinishLaunchingWithOptions.
- Set the callback handler on the channel.
- Use the native APIs to carry out some motion and return the end result.
Some helpful assets on Platform channel: