尝试在现有的flutter应用程序中实现flutter_contacts示例 [英] Trying to implement flutter_contacts example in existing flutter app

查看:137
本文介绍了尝试在现有的flutter应用程序中实现flutter_contacts示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现此处的flutter_contacts示例:

I am trying to implement the flutter_contacts example that lives here: flutter contacts in my existing flutter application but im having issues with calling the kotlin method. I have mirrored the functionality completely as far as I can tell but when I call the launch contacts method it throws a missing plugin exception. Here is the error:

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应用程序中运行良好,因此我假设我缺少某些东西.

It works fine in the expmle application when i run it so im assuming i'm missing something.

主要活动代码:

 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(() {
  });
 }

推荐答案

setMethodCallHandler 内,您必须编写一个if语句来检查方法名称,然后执行它:

Inside the setMethodCallHandler you must write an if statement to check the method name and then execute it:

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

这篇关于尝试在现有的flutter应用程序中实现flutter_contacts示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆