【问题标题】:Trying to implement flutter_contacts example in existing flutter app尝试在现有的 Flutter 应用中实现 flutter_contacts 示例
【发布时间】:2020-03-14 05:34:08
【问题描述】:

我正在尝试在我现有的 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
}
 }

要启动的 Dart 代码

   void launchContacts() async {
   try {
    await platform.invokeMethod('launch');
   } on PlatformException catch (e) {
    print("Failed to launch contacts: ${e.message}");
   }
  setState(() {
  });
 }

【问题讨论】:

    标签: android kotlin flutter dart


    【解决方案1】:

    setMethodCallHandler中你必须写一个if语句来检查方法名然后执行它:

    if (call.method == "launch")
         launchContactActivity()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-01-16
      • 1970-01-01
      • 1970-01-01
      • 2019-11-16
      • 2020-07-13
      • 2011-10-31
      • 2013-07-19
      相关资源
      最近更新 更多