简体   繁体   English

如何在 Flutter 中运行 Kotlin 函数?

[英]How can I run Kotlin function in Flutter?

I followed what Flutter did in https://flutter.dev/docs/development/platform-integration/platform-channels but it didn't work.我遵循了 Flutter 在https://flutter.dev/docs/development/platform-integration/platform-channels 中所做的,但没有奏效。 I checked other people's work and still nothing.我检查了其他人的工作,仍然没有。 So what do you guys suggest?那么大家有什么建议呢?

class MainActivity: FlutterFragmentActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    GeneratedPluginRegistrant.registerWith(this.getFlutterEngine()!!)
}


private val CHANNEL = "samples.flutter.dev/battery"

override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
    super.configureFlutterEngine(flutterEngine)
    MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler {
        call, result ->
        if (call.method == "MyMethod"){
            result.success("Hello Kotlin")
        }
    }
}}

and this is the method in flutter itself这就是颤振本身的方法

static const platform = 
const MethodChannel("samples.flutter.dev/battery");


Future<void> MyMethod() async {
String value = "";

try{
  value = await platform.invokeMethod("MyMethod");
}
catch(e){
  print(e);
}
print(value);
}

Please, I've tried everything on internet but none of them worked.拜托,我已经在互联网上尝试了一切,但没有一个奏效。 I beg your help for my case.我请求你帮助我的案子。

I just solved my issue.我刚刚解决了我的问题。 It was in AndroidManifest.xml它在 AndroidManifest.xml 中

it was它是

 android:name="io.flutter.embedding.android.FlutterFragmentActivity"

I changed it back to我把它改回

   android:name=".MainActivity"

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

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