Create and share contact vcf Flutter

60 Views Asked by At

I want to create a vcf file for a contact on runtime and share it.

//Takes a [contact] and returns its VCF file
  static Future<XFile> _getVCardFile(Contact contact) async {
    Directory tempDir = await getTemporaryDirectory();

    final vCardString = contact.toVCard();
    final filePath = '${tempDir.path}/contact_${contact.id}.vcf';
    return XFile(
      filePath,
      bytes: Uint8List.fromList(vCardString.codeUnits),
    );    
  }

  static Future<void> shareContacts(Iterable<Contact> contacts) async {
    final filePaths = <XFile>[];
    for (final c in contacts) {
      final file = await _getVCardFile(c);
      filePaths.add(file);
    }

    await Share.shareXFiles(filePaths);
  }

By the above code I am expecting that temporary cached vcf files for the contacts are created and user is able to share them(same as on default contacts app). But I get the debug error: "PlatformException (PlatformException(Share failed, /data/user/0/com.example.contacts/cache/contact_3091.vcf: The source file doesn't exist., null, null))"

0

There are 0 best solutions below