Search code examples
flutterdartdart-async

How to call random number from contact list? [Flutter]


Using FlutterPhoneDirectCaller, I'm looking for a way to randomly selected phone number from contact list and make a direct call.

getContacts() worked properly getting all the phone numbers from my phone, though I'm having trouble making var list for randomly selecting number from the list.

import 'package:permission_handler/permission_handler.dart';
import 'package:contacts_service/contacts_service.dart';
import 'package:flutter_phone_direct_caller/flutter_phone_direct_caller.dart';
    
    var contacts = ContactsService.getContacts();
    var list = [contacts];
      final _random = new Random();
    var element = list[_random.nextInt(list.length)];
    
    void checkContactPermission() async {
      var status = await Permission.contacts.status;
      if (status.isGranted) {
        FlutterPhoneDirectCaller(_random.nextInt(list.length));
      }

Solution

  • After getting the contacts, you need to pass number on FlutterPhoneDirectCaller. Based on documentation, you need to use like.

      const number =  _random.nextInt(list.length - 1);
      bool res = await FlutterPhoneDirectCaller.callNumber(number);