3

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!

Ahmad Aghazadeh
  • 16,571
  • 12
  • 101
  • 98
Hasan A Yousef
  • 22,789
  • 24
  • 132
  • 203

3 Answers3

3

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
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
1

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

David Morgan
  • 466
  • 3
  • 6
0

You can see this project: Full Sample

In Andriod:

class MainActivity: FlutterActivity() {
    private val DATA_CHANNEL = "app.channel.shared.data"
    override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
        GeneratedPluginRegistrant.registerWith(flutterEngine);
        MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), DATA_CHANNEL).setMethodCallHandler { call, result ->
            if (call.method!!.contentEquals("getSharedText")) {
                result.success("Shared Text")
            } 
        }
    }
}

In Dart:

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void main() {
   WidgetsFlutterBinding.ensureInitialized();
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _MyAppState();
  }
}

class _MyAppState extends State<MyApp> {

  static const platform = const MethodChannel('app.channel.shared.data');
  String dataShared = "No Data";

@override
  void initState() {
    super.initState();
    getSharedText();
  }

  getSharedText() async {
    var sharedData = await platform.invokeMethod("getSharedText");
    if (sharedData != null) {
      setState(() {
        dataShared = sharedData;
      });
    }
  }
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home:Scaffold(body: Center(child: Text(dataShared))) 
    );
  }
}
Ahmad Aghazadeh
  • 16,571
  • 12
  • 101
  • 98