Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I call Kotlin function from Dart

Using Flutter, a kotlin/swift function can be called by something like:

file.dart:

static const platform = const MethodChannel('my.test.flutterapp/battery');
final int result = await platform.invokeMethod('getBatteryLevel');

file.kt:

private val CHANNEL = "my.test.flutterapp/battery"
MethodChannel(flutterView, CHANNEL).setMethodCallHandler { call, result ->
          if (call.method == "getBatteryLevel") {
              ...
          } else {
              result.notImplemented()
          }
      }

Is there something similar to call Kotlin function from standard Dart app, like Dart console app!

like image 737
Hasan A Yousef Avatar asked Feb 10 '26 04:02

Hasan A Yousef


2 Answers

https://flutter.io/developing-packages/

Plugin packages: A specialized Dart package which contain an API written in Dart code combined with a platform-specific implementation for Android (using Java or Kotlin), and/or for iOS (using ObjC or Swift). A concrete example is the battery plugin package.

...

flutter create --template=plugin -i swift -a kotlin hello
like image 197
Günter Zöchbauer Avatar answered Feb 16 '26 15:02

Günter Zöchbauer


For the VM the mechanisms available are basic OS operations and native extensions.

By OS operations I mean, you could launch a separate process and interact with it, using files or the network stack. This is probably not as fine grained as you're looking for.

Native extensions allow you to call out to C or C++ code. I don't know enough about kotlin to know if you can easily expose functionality to C/C++. If it's possible, this will give you the tightest integration.

https://www.dartlang.org/articles/dart-vm/native-extensions

like image 40
David Morgan Avatar answered Feb 16 '26 15:02

David Morgan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!