如何在颤振中从联系人列表中随机选择号码?
How to call randomly selected number from contact list in flutter?
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();
}
}
}
这是我的代码,用于呼叫从联系人列表中随机选择的号码,但它正在呼叫一个甚至不在联系人列表中的号码。我应该改变什么?
提前致谢
可以使用Flutter中的dart:math包生成一个介于1和contactList大小之间的随机数,然后通过生成的随机索引调用随机联系人即可。
import 'dart:math';
getRandomContact(List<Contact> contactsList){
var random = new Random();
var randomContactIndex = random.nextInt(contactsList.length);
return contactsList[randomContactIndex];
}
你可以这样称呼它:
Contact randomContact = getRandomContact(contactsList);
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();
}
}
}
这是我发现的有效方法
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();
}
}
}
这是我的代码,用于呼叫从联系人列表中随机选择的号码,但它正在呼叫一个甚至不在联系人列表中的号码。我应该改变什么? 提前致谢
可以使用Flutter中的dart:math包生成一个介于1和contactList大小之间的随机数,然后通过生成的随机索引调用随机联系人即可。
import 'dart:math';
getRandomContact(List<Contact> contactsList){
var random = new Random();
var randomContactIndex = random.nextInt(contactsList.length);
return contactsList[randomContactIndex];
}
你可以这样称呼它:
Contact randomContact = getRandomContact(contactsList);
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();
}
}
}
这是我发现的有效方法