Reputation: 63
Ive been reading a lot of discussion regarding saving a contact to mobile from web.The only available is for IOS/Android. im currently working on a Flutter web project were i have "save to contact" button.Can anyone please help me?
Upvotes: 1
Views: 913
Reputation: 4007
I'm saving the contact information as Vcard in flutter web with the following code without any extra plugins. This will save the contacts to all devices.
Note: I used Vcard version 1.0, but that was complex and it didn't saved to contacts on Android. Only V3 is working without errors.
Tested on android, iOS (Safari only), macOS, windows
import 'dart:html' as html;
Future<void> addToContactsFn() async {
final String vCard3Text = '''
BEGIN:VCARD
VERSION:3.0
FN:${d.name}
UID:${d.id}
EMAIL:${d.email ?? '-'}
TEL;TYPE=WORK:${d.phoneNumber}
URL:${d.website ?? '-'}
NOTE:Created on ${AppInfo.appName}
N:${d.name};;;;;
END:VCARD''';
final html.Blob blob = html.Blob(<String>[vCard3Text]);
final String url = html.Url.createObjectUrlFromBlob(blob);
final html.AnchorElement _ = html.AnchorElement(href: url)
..target = 'blank'
..download = '${d.name}.vcf'
..click();
html.Url.revokeObjectUrl(url);
}
Upvotes: 1