【问题标题】:Not able to add contact using contacts_service in flutter app无法在颤振应用程序中使用contacts_service添加联系人
【发布时间】:2020-09-17 17:28:59
【问题描述】:

实际上,我正在构建一个读卡器应用程序,其中数据来自 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

我/颤振(1615):/data/user/0/com.example.camera_app/app_flutter/2020-05-29 23:35:12.398628.png 我/颤动(1615):图像 我/颤振(1615):404 I/颤动(1615):图像发送 我/颤振(1615):形式来听单接触 我/颤振(1615):保存 我/颤动(1615):fi 我/颤动(1615):fi2 我/颤动(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(包:flutter/src/services/platform_channel.dart:329:12) E/flutter (1615):#3 ContactsService.addContact (package:contacts_service/contacts_service.dart:83:16) E/颤振(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/颤振(1615):#6 _InkResponseState.build。 (包:flutter/src/material/ink_well.dart:862:36) E/flutter(1615):#7 GestureRecognizer.invokeCallback(包: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 (package:flutter/src/gestures/tap.dart:217:7) E/flutter(1615):#11 PrimaryPointerGestureRecognizer.handleEvent(包:flutter/src/gestures/recognizer.dart:475:9) E/flutter(1615):#12 PointerRouter._dispatch(包:flutter/src/gestures/pointer_router.dart:76:12) E/颤振(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(包: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(包:flutter/src/gestures/binding.dart:218:19) E/flutter(1615):#18 GestureBinding.dispatchEvent(包:flutter/src/gestures/binding.dart:198:22) E/flutter(1615):#19 GestureBinding._handlePointerEvent(包:flutter/src/gestures/binding.dart:156:7) E/flutter(1615):#20 GestureBinding._flushPointerEventQueue(包:flutter/src/gestures/binding.dart:102:7) E/flutter (1615): #21 GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:86:7)

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    实际上我错过了权限部分。哦,我是 Flutter 的新手。

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

    所以让我们使用contacts_service 0.4.6。 首先,添加依赖为:

    dependencies:  
    contacts_service: ^0.4.6
    

    权限:

    对于安卓 将以下权限添加到您的 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);
        }
      }
    

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

    【讨论】:

      猜你喜欢
      • 2014-08-06
      • 1970-01-01
      • 1970-01-01
      • 2020-09-03
      • 1970-01-01
      • 1970-01-01
      • 2021-09-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多