我正在尝试在现有的flutter应用程序中实现位于以下位置的flutter_contacts示例:flutter contacts,但是在调用kotlin方法时遇到了问题。据我所知,我已经完全镜像了该功能,但是当我调用启动联系人方法时,它将引发缺少的插件异常。这是错误:
E/flutter (10095): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: MissingPluginException(No implementation found for method launch on channel flutter_contacts/launch_contacts)
当我运行它时,它在expmle应用程序中可以正常工作,因此我假设我缺少某些东西。
主要活动代码:
package com.lightbridge.flutter_contacts
import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.provider.ContactsContract
import io.flutter.app.FlutterActivity
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugins.GeneratedPluginRegistrant
class MainActivity : FlutterActivity() {
var lastResult: MethodChannel.Result? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
GeneratedPluginRegistrant.registerWith(this)
MethodChannel(flutterView, CHANNEL).setMethodCallHandler { call, result ->
lastResult = result
launchContactActivity()
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == REQUEST_CODE) {
if (resultCode == Activity.RESULT_OK) {
lastResult?.success("Done!")
} else {
lastResult?.error("Error", "Can't launch contacts", "")
}
}
}
private fun launchContactActivity() {
val intent = Intent(Intent.ACTION_VIEW)
intent.type = ContactsContract.Contacts.CONTENT_TYPE
startActivityForResult(intent, REQUEST_CODE)
}
companion object {
private const val CHANNEL = "flutter_contacts/launch_contacts"
private const val REQUEST_CODE = 42
}
}
要启动的飞镖代码
void launchContacts() async {
try {
await platform.invokeMethod('launch');
} on PlatformException catch (e) {
print("Failed to launch contacts: ${e.message}");
}
setState(() {
});
}
答案 0 :(得分:0)
在 setMethodCallHandler 内,您必须编写一个if语句来检查方法名称并执行它:
if (call.method == "launch")
launchContactActivity()