Our website uses cookies to ensure that we give you the best experience and to personalise content. If you accept these conditions or continue to use this site, it will mean that you agree with ourPrivacy Policy.

post

Platform-specific code in Flutter

Mateusz
Mateusz, Flutter Developer
08.08.2022

Cross-platform application development is the focus of the Flutter SDK. It is simple to create Android, iOS, and Web applications using a single codebase. You will not need to develop platform-specific code for simple projects. You may easily design your application using packages created by community developers.

But what should you do if your app needs a platform-specific code?

Plugins

For developers to use and build applications, there are countless plugins available at https://pub.dev/. Since a developer does not need to put in a lot of effort to utilize plugins to their maximum capacity, using them is simple and convenient.

You must include a plugin in the pubspec.yaml file in order to use it in your project, for example:
share_plus: ^4.0.10

Make sure that a problem hasn't already been solved before you start working on a solution. Avoid creating something entirely new. With the help of the many plugins available on pub.dev, you can connect Bluetooth devices, utilize platform-specific features like sharing content via the share dialog, and more. Look through the available plugins to see if you can find what you require.

Custom code

Developers occasionally have to come up with solutions to issues that are either not currently addressed by plugins or call for unique approaches. Developers should use platform channels to add platform-specific implementations when they are necessary.

Messages are sent from the host (platform) to the client (UI) using platform channels. Messages and responses are exchanged asynchronously to keep the user interface responsive. MethodChannel enables client-side message that matches method calls. Platform-specific methods allow for receiving method calls and returning results.

Developers can send and accept values like booleans, integers, and Strings that are simple JSON-like values.

How to create platform-specific code?

You need to start by making a basic Flutter application or use a project that you are working on currently. The platform folder for which you want to implement the code should then be entered (/android, /ios/, etc.). For our example, we will use platform-specific code for Android.

  • Create a variable in the MainActivity.kt file to hold the name of your channel.
  • Initiate a MethodChannel then use the setMethodCallHandler function inside the configureFlutterEngine method. 
  • There, native functionality can be implemented and call method names can be accessed to determine which method should be executed. 
  • Once you've obtained the required data, you can send it back by passing the argument to the result.success(data) function.
  • You can use native functionality inside your Flutter application after implementing it. 
  • Use the channel name you declared in MainActivity.kt to create MethodChannel. 
  • Get results by using invokeMethod(name) on the MethodChannel that was previously constructed. 
  • The name of the argument should be the same as the name of the platform-specific function that was implemented.

The same processes are necessary for Windows and iOS as well as other platforms. For additional details, refer to the guidelines provided at https://docs.flutter.dev/development/platform-integration/platform-channels.

Conclusion

Developers are not constrained by Flutter's restrictions because it offers options to integrate native functionalities. PlatformChannels can be used to implement anything that can only be done on iOS or Android for Flutter.

Anything possible on native platforms can be included in the Flutter application.

Looking for support with your digital product?

Drop us a line at contact@softnauts.com and a friendly member of our team will get back to you shortly.

newsletter

Stay updated with new posts

Get notifications when new articles are posted. You can always unsubscribe from the list.

Softnauts is committed to processing the above information. Read Privacy Policy

Subscribe