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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了无法在flutter应用程序中使用contacts_service添加联系人相关的知识,希望对你有一定的参考价值。

实际上,我正在构建一个读卡器应用程序,其中的数据来自ML模型,并存储在mycontrollers函数中。但是在使用ContactsService.addContact(contact);时我出错了。尽管联系人已保存在应用程序中,但未保存在手机的联系人列表中。

代码:

 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.pngI / flutter(1615):影像I /颤振(1615):404I / flutter(1615):图像发送I / flutter(1615):表单来监听I /颤振(1615):保存I /颤振(1615):fiI /颤振(1615):fi2I /颤振(1615):对象E / flutter(1615):[错误:flutter / lib / ui / ui_dart_state.cc(157)]未处理的异常:FormatException:无效的信封E / flutter(1615):#0 StandardMethodCodec.decodeEnvelope(package:flutter / src / services / message_codecs.dart:571:7)E / flutter(1615):#1 MethodChannel._invokeMethod(软件包:flutter / src / services / platform_channel.dart:156:18)E /颤振(1615):E / flutter(1615):#2 MethodChannel.invokeMethod(package:flutter / src / services / platform_channel.dart:329:12)E / flutter(1615):#3 ContactsService.addContact(package:contacts_service / contacts_service.dart:83:16)E / flutter(1615):#4 _FormState.saveData(package: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(package:flutter / src / gestures / recognizer.dart:182:24)E / flutter(1615):#8 TapGestureRecognizer.handleTapUp(包:flutter / src / gestures / tap.dart:504:11)E / flutter(1615):#9 BaseTapGestureRecognizer._checkUp(包:flutter / src / gestures / tap.dart:282:5)E / flutter(1615):#10 BaseTapGestureRecognizer.handlePrimaryPointer(包: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._dispatch(软件包:flutter / src / gestures / pointer_router.dart:76:12)E / flutter(1615):#13 PointerRouter._dispatchEventToRoutes。 (包:flutter / src / gestures / pointer_router.dart:122:9)E / flutter(1615):#14 _LinkedHashMapMixin.forEach(dart:collection-patch / compact_hash.dart:379:8)E / flutter(1615):#15 PointerRouter._dispatchEventToRoutes(package:flutter / src / gestures / pointer_router.dart:120:18)E / flutter(1615):#16 PointerRouter.route(package:flutter / src / gestures / pointer_router.dart:106:7)E / flutter(1615):#17 GestureBinding.handleEvent(package:flutter / src / gestures / binding.dart:218:19)E / flutter(1615):#18 GestureBinding.dispatchEvent(package:flutter / src / gestures / binding.dart:198:22)E / flutter(1615):#19 GestureBinding._handlePointerEvent(package:flutter / src / gestures / binding.dart:156:7)E / flutter(1615):#20 GestureBinding._flushPointerEventQueue(package:flutter / src / gestures / binding.dart:102:7)E / flutter(1615):#21 GestureBinding._handlePointerDataPacket(package:flutter / src / gestures / binding.dart:86:7)

答案

实际上,我缺少权限部分。哦,我真是Flutter的新手。

这最后对我有用,因为它可以将联系人保存在MAin联系人或电话目录中。

因此,我们使用contacts_service 0.4.6。首先,将依赖项添加为:

dependencies:  
contacts_service: ^0.4.6

权限:

对于android将以下权限添加到您的AndroidManifest.xml:

<uses-permission android:name="android.permission.READ_CONTACTS" />  
<uses-permission android:name="android.permission.WRITE_CONTACTS" />  

对于ios在您的Info.plist文件中设置NSContactsUsageDescription

<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);
    
  

这将保存联系人并在当前应用程序中进行更改时编辑联系人。

以上是关于无法在flutter应用程序中使用contacts_service添加联系人的主要内容,如果未能解决你的问题,请参考以下文章

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

Flutter:如何从联系人获取用户地址

在Elementor中使用Contact Form 7但无法正确对齐文本框

无法修改 didBeginContact 中的contact.bodyA

无法在 Flutter Web 视图中使用本地存储

无法在 Flutter 中使用 .map 函数