contactsService static method

String contactsService(
  1. String projectName
)

Implementation

static String contactsService(String projectName) {
  return '''
import 'package:flutter_contacts/flutter_contacts.dart' as fc;
import 'package:permission_handler/permission_handler.dart';
import 'package:$projectName/core/modules/contacts/contact.dart';
import 'package:$projectName/core/services/feature_service.dart';

class PhoneContactsService {
static Future<Contact?> pickContact() async {
  if (!FeatureService.isContactsEnabled) {
    throw Exception('Contacts feature is disabled. Enable it in settings.');
  }

  final status = await Permission.contacts.request();
  if (!status.isGranted) {
    throw Exception('Contacts permission denied.');
  }

  try {
    final fc.Contact? picked = await fc.FlutterContacts.openExternalPick();
    if (picked == null) return null;

    final name = picked.displayName.isNotEmpty
        ? picked.displayName
        : 'Unknown';
    final phoneNumber = picked.phones.isNotEmpty
        ? picked.phones.first.number
        : '';

    if (phoneNumber.isEmpty) {
      throw Exception('Selected contact has no phone number.');
    }

    return Contact(name: name, phoneNumber: phoneNumber);
  } catch (e) {
    throw Exception('Error picking contact: \$e');
  }
}
}
''';
}