gpt4 book ai didi

android - FlutterActivity MethodChannel 和 FlutterView

转载 作者:行者123 更新时间:2023-12-03 10:55:16 26 4
gpt4 key购买 nike

所以我在大约 4 个月前写了一个 Flutter 应用程序。现在我想做一个小改动,但我不能再编译应用程序了,因为 GeneratedPluginRegistrant.registerWith(this) 不再起作用,我没有更改 Kotlin 代码,只更改了 Dart 代码。

“GeneratedPluginRegistrant.registerWith( 这个)”中的“ 这个”向我显示了这个错误:

Type mismatch.    
Required: FlutterEngine!
Found: MainActivity

MainActivity 类:
import android.os.Bundle
import io.flutter.app.FlutterActivity
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugins.GeneratedPluginRegistrant
import io.flutter.view.FlutterMain

class MainActivity : FlutterActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

GeneratedPluginRegistrant.registerWith(this) // here is the error: Type mismatch. Required: FlutterEngine! Found: MainActivity

MethodChannel(flutterView, CHANNEL).setMethodCallHandler { call, result ->
if (call.method == "helloFromNativeCode") {
val greetings = helloFromNativeCode()
result.success(greetings)
}
}
}

private fun helloFromNativeCode(): String {
return "Hello from Native Android Code"
}

companion object {
private const val CHANNEL = "flutter.native/helper"
}
}

如果是使用:
import io.flutter.embedding.android.FlutterActivity

代替
import io.flutter.app.FlutterActivity

我可以用
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
GeneratedPluginRegistrant.registerWith(flutterEngine);
}

但遇到以下问题:
MethodChannel(flutterView, CHANNEL).setMethodCallHandler { call, result ->
if (call.method == "helloFromNativeCode") {
val greetings = helloFromNativeCode()
result.success(greetings)
}
}

因为我在 上遇到错误 flutter 查看 :
Unresolved reference: flutterView

代码如下所示:
import android.os.Bundle
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugins.GeneratedPluginRegistrant
import io.flutter.view.FlutterMain

class MainActivity : FlutterActivity() {
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
GeneratedPluginRegistrant.registerWith(flutterEngine);
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

MethodChannel(flutterView, CHANNEL).setMethodCallHandler { call, result -> // here is the error
if (call.method == "helloFromNativeCode") {
val greetings = helloFromNativeCode()
result.success(greetings)
}
}
}

private fun helloFromNativeCode(): String {
return "Hello from Native Android Code"
}

companion object {
private const val CHANNEL = "flutter.native/helper"
}
}

我希望有一个人可以帮助我。

最佳答案

代替flutterView 使用flutterEngine.getDartExecutor()。

关于android - FlutterActivity MethodChannel 和 FlutterView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59323505/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com