How to create the contact in locally with Name and phone number and share the created contact to other apps like whatsapp, social media's etc...
By using vcard_maintained
library https://pub.dev/packages/vcard_maintained, we are able to create the contact, But not able to share through the apps.
I tried this,
import 'package:share_plus/share_plus.dart';
import 'package:vcard_maintained/vcard_maintained.dart';
var vCard = VCard();
vCard.firstName = 'FirstName';
vCard.middleName = 'MiddleName';
vCard.workPhone = '312-555-1212';
final path = await _localPath;
vCard.saveToFile('$path/contact.vcf');
Share.shareFiles(['$path/contact.vcf'], text: 'Great picture');
Future<String> get _localPath async {
final directory = await getApplicationDocumentsDirectory();
return directory.path;
}
But getting format of this vcard is not support
error.
This is the working example.
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:path_provider/path_provider.dart';
import 'package:share_plus/share_plus.dart';
import 'package:vcard_maintained/vcard_maintained.dart';
void shareAllVCFCard(BuildContext context, {required List<VCard> vCard}) async {
try {
List<String> vcsCardPath = <String>[];
int index = 0;
for (final card in vCard) {
index++;
var vCardAsString = card.getFormattedString();
final directory = await getApplicationDocumentsDirectory();
final path = directory.path;
var pathAsText = "$path/$index.txt";
var contactAsFile = File(await getFilePath(index.toString()));
contactAsFile.writeAsString(vCardAsString);
var vcf = contactAsFile
.renameSync(contactAsFile.path.replaceAll(".txt", ".vcf"));
vcsCardPath.add(vcf.path);
}
Share.shareFiles(vcsCardPath, text: 'Great picture');
} catch (e) {
print("Error Creating VCF File $e");
return null;
}
}
Future<String> getFilePath(String fileName) async {
Directory appDocumentsDirectory =
await getApplicationDocumentsDirectory(); // 1
String appDocumentsPath = appDocumentsDirectory.path; // 2
String filePath = '$appDocumentsPath/$fileName.txt'; // 3
return filePath;
}