Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 无法理解如何将代码放入kotlin并使用方法通道从颤振发送函数_Flutter_Kotlin_Dart - Fatal编程技术网

Flutter 无法理解如何将代码放入kotlin并使用方法通道从颤振发送函数

Flutter 无法理解如何将代码放入kotlin并使用方法通道从颤振发送函数,flutter,kotlin,dart,Flutter,Kotlin,Dart,My MainActivity.kt: class MainActivity : FlutterActivity() { private val CHANNEL = "flutter.native/helper" private var mapId: Int? = null override fun onCreate(savedInstanceState: Bundle?, PersistentState: PersistableBundle?)

My MainActivity.kt:

class MainActivity : FlutterActivity() {

    private val CHANNEL = "flutter.native/helper"
    private var mapId: Int? = null

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

        GeneratedPluginRegistrant.registerWith(FlutterEngine(this));

    }
    
    override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
        GeneratedPluginRegistrant.registerWith(flutterEngine);
        
        MethodChannel(flutterEngine.dartExecutor, CHANNEL).setMethodCallHandler { call, result ->
            if (call.method == "KML") {
                result.success(getKMLResource());
            } else {
                result.notImplemented();
            }
        }
    }

    private fun getKMLResource(): Int {
        return R.raw.borders;
    }
}
我正在尝试插入下面的调用,但每次都出现错误

override fun onMethodCall(call: MethodCall, result: Result) {
    when (call.method) {
        "showToast" -> {
            val text = call.argument<String>("text") // hello world
            showToast(text)
        }
    }
}
错误:

'onMethodCall' overrides nothing
One type argument expected for class Result<out T>
Unresolved reference: showToast
这是我的颤振部分:

  Future<void> printSomething(GoogleMapController mapController) async {
    const MethodChannel channel = MethodChannel('flutter.native/helper');
    channel.invokeMethod('showToast', {'text': 'hello world'});
  }
我不知道如何将它正确地合并到我的kotlin代码中,并且没有太多的例子。至少我找不到。我的最终目标是能够操纵私有的fun getKMLResource,并将return设置为我从flatter应用程序中选择的目录

有人知道怎么做吗?请帮忙。多谢各位

下面是我的添加kml颤振功能:

以下是我如何选择我的.kml:

  Future<void> addKml(GoogleMapController mapController) async {
    const MethodChannel channel = MethodChannel('flutter.native/helper');
    try {
      int kmlResourceId = await channel.invokeMethod('KML');
      return mapController.channel.invokeMethod("map#addKML", <String, dynamic>{
        'resourceId': kmlResourceId,
      });
    } on PlatformException catch (e) {
      throw 'Unable to plot map: ${e.message}';
    }
  }

首先,你必须遵循以下文件:

错误:

“onMethodCall”不覆盖任何内容

意味着您正在尝试覆盖不存在的内容

在这种情况下,您必须实现文档教程中缺少的类:

ActivityAware, FlutterPlugin, MethodChannel.MethodCallHandler
关于:

MethodChannel.MethodCallHandler

您可以覆盖Kotlin代码段:

override fun onMethodCall(call: MethodCall, result: MethodChannel.Result)
这会解决你的问题

ActivityAware和FlatterPlugin是新的Flatter升级的基础,您可以使用它们有效地修复内存泄漏

class BubbleOverlayPlugin : ActivityAware, FlutterPlugin, MethodChannel.MethodCallHandler {


private var activity: Activity? = null
private var channel: MethodChannel? = null


   override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {
    when (call.method) {
       //here lies the platform methods calls
    }
   }

    override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) {
        channel?.setMethodCallHandler(null)
        //release resources
    }

    override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
        channel = MethodChannel(flutterPluginBinding.binaryMessenger, channelName)
        channel?.setMethodCallHandler(this)
    }

    override fun onAttachedToActivity(binding: ActivityPluginBinding) {
        activity = binding.activity
    }

    override fun onDetachedFromActivity() {
        //release resources
    }

    override fun onReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) {
        activity = binding.activity
    }

    override fun onDetachedFromActivityForConfigChanges() {
        //release resources
    }
}
你可以从插件bubble_overlay查看我的源代码,它使用的是最后的文档推荐


回购:非常感谢您的回复。。。我会在明天早上试一试,然后再打电话给你。我还在为此挣扎,但我还没有放弃。除了错误和头痛,我什么都没有onMethodCall'不覆盖任何内容。未解析的引用:showtoos@_@电池电量和负载都有。kml一起工作。。。我用的是同一个频道。现在我仍在尝试如何使用文件选择器在kotlin中选择一个特定的.kml。你知道我是否可以跳过所有kotlin的胡说八道,通过flutter调用我的.kml文件选择器?我在上面添加了我的flatter.kml函数。