dart 在弄清楚如何将代码放入Kotlin并使用方法通道从flutter发送函数时遇到困难

chhqkbe1  于 2023-06-27  发布在  Kotlin
关注(0)|答案(2)|浏览(140)

我的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

这是我的flat部分:

Future<void> printSomething(GoogleMapController mapController) async {
    const MethodChannel channel = MethodChannel('flutter.native/helper');
    channel.invokeMethod('showToast', {'text': 'hello world'});
  }

我不知道如何将它正确地合并到我的Kotlin代码中,并且没有太多的例子。至少我找不到。我的最终目标是能够操作私有的fun getKMLResource()并将return设置为我从flutter应用程序中选择的目录。
有人知道怎么做吗?请帮帮我谢谢你。
下面是我的add kml flutter函数:
下面是我如何选择我的.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}';
    }
  }
kmbjn2e3

kmbjn2e31#

首先,你必须遵循这些文件:
Writing custom platform-specific code
Supporting the new Android plugins APIs
错误:
“onMethodCall”不覆盖任何内容
表示您正在尝试覆盖不存在的内容
在这种情况下,你必须实现在docs教程中找到的缺少的类:

ActivityAware, FlutterPlugin, MethodChannel.MethodCallHandler

日期:
MethodChannel.MethodCallHandler
你可以重写(Kotlin代码段):

override fun onMethodCall(call: MethodCall, result: MethodChannel.Result)

这应该解决你的问题。
ActivityAware和FlutterPlugin是新flutter升级的必备品,您可以使用它们有效地修复内存泄漏。

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检查我的源代码,它使用的是最后的文档建议.
回购:bubble_overlay repo

bsxbgnwa

bsxbgnwa2#

假设您使用的是Android Studio,您只需在项目顶部打开项目,而不是在android/文件夹中打开项目。

相关问题