简体   繁体   English

我可以从 Dart 调用 Kotlin 函数吗

[英]Can I call Kotlin function from Dart

Using Flutter , a kotlin/swift function can be called by something like:使用Flutter ,可以通过以下方式调用 kotlin/swift 函数:

file.dart : file.dart :

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

file.kt : 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!有没有类似从标准Dart应用程序调用Kotlin函数的东西,比如 Dart 控制台应用程序!

https://flutter.io/developing-packages/ 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).插件包:一个专门的 Dart 包,其中包含一个用 Dart 代码编写的 API,并结合了针对 Android(使用 Java 或 Kotlin)和/或针对 iOS(使用 ObjC 或 Swift)的平台特定实现。 A concrete example is the battery plugin package.一个具体的例子是电池插件包。

... ...

 flutter create --template=plugin -i swift -a kotlin hello

For the VM the mechanisms available are basic OS operations and native extensions.对于 VM,可用的机制是基本的 OS 操作和本机扩展。

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.本机扩展允许您调用 C 或 C++ 代码。 I don't know enough about kotlin to know if you can easily expose functionality to C/C++.我对 kotlin 知之甚少,不知道您是否可以轻松地向 C/C++ 公开功能。 If it's possible, this will give you the tightest integration.如果可能,这将为您提供最紧密的集成。

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

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))) 
    );
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM