当前位置: 首页 > 知识库问答 >
问题:

无法在flutter应用程序中使用contacts_service添加联系人

窦英武
2023-03-14
 void saveContactInPhone() {
try {

  print("saving Conatct");
  Contact contact = Contact();
    print("fi");
    contact.displayName = myController1.text ; 
    contact.phones = [Item(label: "mobile", value: myController4.text)];
    // contact.emails = [Item(label: "email", value: myController3.text)];
    contact.company = myController2.text;
    print("fi2");

  ContactsService.addContact(contact);
  print("object");      

} catch (e) {
  print(e);
}

错误:

I/flutter ( 1615): form init

I/flutter(1615)://data/user/0/com.example.camera_app/app_flutter/2020-05-29 23:35:12.398628.PNG I/flutter(1615):Going Image I/flutter(1615):404 I/flutter(1615):Image Send I/flutter(1615):form come listenForSinglecontact I/flutter(1615):saving I/flutter(1615):fi I/flutter(1615):fi I/flutter(1615):fi I/flutter(1615):fi I/flutter(1615):Flutter(1615):#0 StandardMethodCodec.DecodeEnvelope(包:Flutter/SRC/Services/Message_Codecs.Dart:571:7)E/Flutter(1615):#1 MethodChannel._InvokeMethod(包:Flutter/SRC/Services/Platform_Channel.Dart:156:18)E/Flutter(1615):E/Flutter(1615):#2 MethodChannel.InvokeMethod(包:Flutter/SRC/Services/Platform_Channel.Dart:329:12)E/Flutter(1615):#3 E/Flutter(1615):#4_formstate.savedata(包:camera_app/screens/form.dart:249:23)E/Flutter(1615):#5_INKResponseState._Handletap(包:flutter/src/Material/INK_Well.dart:779:19)E/flutter(1615):#6_INKResponseState.build。(包:flutter/src/material/ink_well.dart:862:36)e/flutter(1615):#7 gesturerecognizer.invokecallback(包:flutter/src/gestures/recogizer.dart:182:24)e/flutter(1615):#8 tapgesterecognizer.handletapup(包:flutter/src/gestures/tap.dart:504:11)e/flutter(1615):#9 basetapgesterecognizer.HandlePrimaryPointer(package:flutter/src/gestures/tap.dart:217:7)E/Flutter(1615):#11 primarypointergesturerecognizer.handleEvent(package:flutter/src/gestures/recognizer.dart:475:9)E/Flutter(1615):#12 pointerrouter._dispatcheventtoroutes(package:flutter/src/gestures/pointerrouter.(包:flutter/src/gestures/pointer_router.dart:122:9)E/flutter(1615):#14_LinkedHashmapmixin.foreach(包:collection-patch/compact_hash.dart:379:8)E/flutter(1615):#15 pointerrouter._dispatcheventtoroutes(包:flutter/src/gestures/pointer_router.dart:120:18)E/flutter(1615):#16 pointerrouter.route(包ures/binding.dart:218:19)e/flutter(1615):#18手势绑定。dispatchevent(包:flutter/src/手势/绑定。dart:198:22)e/flutter(1615):#19手势绑定。_handlepointerevent(包:flutter/src/手势/绑定。dart:156:7)e/flutter(1615):#20手势绑定。_flushpointereventqueue(包:flutter/src/手势/绑定。dart:102:7)e/flutter(1615):#21手势绑定。

共有1个答案

富钧
2023-03-14

实际上我遗漏了权限部分。哦,我在Flutter是个新手。

这为我工作,最后为保存在主要联系人或电话目录中的联系人。

因此,让我们对它使用contacts_service0.4.6。首先,将依赖项添加为:

dependencies:  
contacts_service: ^0.4.6
<uses-permission android:name="android.permission.READ_CONTACTS" />  
<uses-permission android:name="android.permission.WRITE_CONTACTS" />  
<key>NSContactsUsageDescription</key>  
<string>This app requires contacts access to function properly.</string>  
import 'package:contacts_service/contacts_service.dart'; 

基本上,

 final myController1 = TextEditingController();
 final myController2 = TextEditingController();
 ...

这些变量用于存储用户在Textfield或TextForm中输入的值。可以使用MyController1.text查询该值。我们还可以将Contact类对象存储为:

   newContact.givenName = "Some name";
            newContact.emails = [
              Item(label: "email", value: "abc@gmail.com")
            ];
            newContact.company = myController2.text;
            newContact.phones = [
              Item(label: "mobile", value: "123456789")
            ];

仅保存联系人的代码

     Future<void> saveContactInPhone() async {
    try {

      print("saving Conatct");
      PermissionStatus permission = await Permission.contacts.status;

    if (permission != PermissionStatus.granted) {
      await Permission.contacts.request();
      PermissionStatus permission = await Permission.contacts.status;

      if (permission == PermissionStatus.granted) {
        Contact newContact = new Contact();
        newContact.givenName = myController1.text;
        newContact.emails = [
          Item(label: "email", value: myController3.text)
        ];
        newContact.company = myController2.text;
        newContact.phones = [
          Item(label: "mobile", value: myController4.text)
        ];
        newContact.postalAddresses = [
          PostalAddress(region: myController6.text)
        ];
        await ContactsService.addContact(newContact);

      } else {
        //_handleInvalidPermissions(context);
      }
    } else {
      Contact newContact = new Contact();
        newContact.givenName = myController1.text;
        newContact.emails = [
          Item(label: "email", value: myController3.text)
        ];
        newContact.company = myController2.text;
        newContact.phones = [
          Item(label: "mobile", value: myController4.text)
        ];
        newContact.postalAddresses = [
          PostalAddress(region: myController6.text)
        ];
        await ContactsService.addContact(newContact);
    }
      print("object");      

    } catch (e) {
      print(e);
    }
  }
Future<void> saveContactInPhone() async {
    try {
      print("saving Conatct");
      PermissionStatus permission = await Permission.contacts.status;

      if (permission != PermissionStatus.granted) {
        await Permission.contacts.request();
        PermissionStatus permission = await Permission.contacts.status;

        if (permission == PermissionStatus.granted) {
          if (widget.checkPrev == 'for_edit') {
            // Contact updatedContact = new Contact();
            Iterable<Contact> updatedContact =
                await ContactsService.getContacts(query: myController1.text);

            Contact updatedContact1 = new Contact();
            updatedContact1 = updatedContact.first;
            await ContactsService.deleteContact(updatedContact1);
            Contact newContact = new Contact();
            newContact.givenName = myController1.text;
            newContact.emails = [
              Item(label: "email", value: myController3.text)
            ];
            newContact.company = myController2.text;
            newContact.phones = [
              Item(label: "mobile", value: myController4.text)
            ];
            newContact.postalAddresses = [
              PostalAddress(region: myController6.text)
            ];
            await ContactsService.addContact(newContact);
          } else if (widget.checkPrev == 'from_btn') {
            Contact newContact = new Contact();
            newContact.givenName = myController1.text;
            newContact.emails = [
              Item(label: "email", value: myController3.text)
            ];
            newContact.company = myController2.text;
            newContact.phones = [
              Item(label: "mobile", value: myController4.text)
            ];
            newContact.postalAddresses = [
              PostalAddress(region: myController6.text)
            ];
            await ContactsService.addContact(newContact);
          }
        }
      } else {
        if (widget.checkPrev == 'for_edit') {
          // Contact updatedContact = new Contact();
          Iterable<Contact> updatedContact =
              await ContactsService.getContacts(query: myController1.text);

          await ContactsService.deleteContact(updatedContact.first);
          // Contact updatedContact1 = new Contact();
          // updatedContact1= updatedContact.first;
          Contact newContact = new Contact();
          newContact.givenName = myController1.text;
          newContact.emails = [Item(label: "email", value: myController3.text)];
          newContact.company = myController2.text;
          newContact.phones = [
            Item(label: "mobile", value: myController4.text)
          ];
          newContact.postalAddresses = [
            PostalAddress(region: myController6.text)
          ];
          await ContactsService.addContact(newContact);
        } else if (widget.checkPrev == 'from_btn') {
          Contact newContact = new Contact();
          newContact.givenName = myController1.text;
          newContact.emails = [Item(label: "email", value: myController3.text)];
          newContact.company = myController2.text;
          newContact.phones = [
            Item(label: "mobile", value: myController4.text)
          ];
          newContact.postalAddresses = [
            PostalAddress(region: myController6.text)
          ];
          await ContactsService.addContact(newContact);
        }
      }
    } catch (e) {
      print(e);
    }
  }
 类似资料:
  • 我想创建一个代码,在没有任何提示的情况下将联系人添加到android联系人列表中,但我得到了这个异常。如果你有另一个想法,我可以添加一个联系人到联系人列表没有任何窗口出现,请与我分享。我是java新手,所以如果你想帮助我展示一个例子或纠正我的代码,请。 logcat: 我也会尝试这个代码,它工作,但一个确认窗口出现

  • 我已经在Windows8.1上安装了PostgreSQL 9.6.2。但pgadmin4无法联系本地服务器。我尝试了stackoverflow中建议的几种解决方案,尝试卸载并重新安装PostgreSQL 9.6.2,尝试修改config.py、config_disro.py并删除漫游文件夹中的文件,尝试了独立的pgadmin4安装,但没有成功。不过,在我的本地机器中,我可以使用psql.exe和日

  • 我有一个java应用程序,需要部署在weblogic服务器中。我目前正在为该应用程序制作ear文件。我的ear文件中有一个ejb jar。我想将log4j2 jar添加到此应用程序中。所以我的文件夹结构是 目前,我已经将JAR放在APP-INF文件夹/lib和META-INF/application中。xml我把JAR放在了模块中。这是我的申请表。xml 但它不接受log4j罐子。有什么解决方案吗

  • 以下是错误: 扑动医生输出: []Flutter(Channel stable,v1.7.8+Hotfix.4,在Linux上,locale en_IN)•Flutter版本1.7.8+Hotfix.4,在/home/sagar/development/Flutter•Framework修订版20E59316B8(两周前),2019-07-18 20:04:33-0700•Engine修订版fee

  • 本文向大家介绍使用 iisext.vbs 添加应用程序依存关系的实现方法,包括了使用 iisext.vbs 添加应用程序依存关系的实现方法的使用技巧和注意事项,需要的朋友参考一下 应用到: Windows Server 2003, Windows Server 2003 R2, Windows Server 2003 with SP1 可以使用命令行脚本 iisext.vbs(存储在 system

  • 向我的gmail帐户添加新联系人时 当我尝试在gmail中找到“添加名称”元素时 - 我无法找到这个元素的xpath,也无法使用selenium webdriver执行click或sendKeys之类的事件。感谢任何帮助 谢谢,奥迪