import 'package:flutter_phone_direct_caller/flutter_phone_direct_caller.dart';
import 'package:contacts_service/contacts_service.dart';
void checkContactPermission() async {
var status = await Permission.contacts.status;
var contacts = ContactsService.getContacts();
var list = [contacts];
var randomItem = (list.shuffle());
if (status.isGranted) {
FlutterPhoneDirectCaller.callNumber('randomItem');
}
if (!status.isGranted) {
PermissionStatus permissionStatus = await Permission.contacts.request();
if (status.isDenied) {
PermissionStatus permissionStatus = await Permission.contacts.request();
}
}
}
Here is my code to call randomly selected number from contact list yet it is calling a same number that isn't even on the contact list. What should I change? Thanks in advance
CodePudding user response:
You can use the dart:math package in Flutter to generate a random number between 1 and the size of the contactList, then just call the random contact by the random index generated.
import 'dart:math';
getRandomContact(List<Contact> contactsList){
var random = new Random();
var randomContactIndex = random.nextInt(contactsList.length);
return contactsList[randomContactIndex);
}
You can call it like:
Contact randomContact = getRandomContact(contactsList);
CodePudding user response:
Future<void> checkContactPermission() async {
var status = await Permission.contacts.status;
if (status.isGranted) {
var contacts = await ContactsService.getContacts();
var list = contacts;
list.shuffle();
var randomItem = (list.first.phones?.first.value);
await FlutterPhoneDirectCaller.callNumber('$randomItem');
}
if (!status.isGranted) {
PermissionStatus permissionStatus = await Permission.contacts.request();
if (status.isDenied) {
PermissionStatus permissionStatus = await Permission.contacts.request();
}
}
}
Here is what I found to be working