Я использую contact_services плагин флаттера для получения контактов из книги мобильных телефонов.
Пока мне просто нужны имена и номер телефона моих контактов. Я успешно получаю Имена, но для номера телефона он печатает Экземпляр ('item') в терминале.
Что мне делать?
Класс телефонной книги Код
import 'package:contacts_service/contacts_service.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:permission_handler/permission_handler.dart';
class PhoneBook extends StatefulWidget {
@override
_PhoneBookState createState() => _PhoneBookState();
}
class _PhoneBookState extends State<PhoneBook> {
List<Contact> _contacts;
@override
initState() {
super.initState();
refreshContacts();
}
Future<PermissionStatus> _getContactPermission() async {
PermissionStatus permission = await PermissionHandler()
.checkPermissionStatus(PermissionGroup.contacts);
if (permission != PermissionStatus.granted &&
permission != PermissionStatus.granted) {
Map<PermissionGroup, PermissionStatus> permissionStatus =
await PermissionHandler()
.requestPermissions([PermissionGroup.contacts]);
return permissionStatus[PermissionGroup.contacts] ??
PermissionStatus.unknown;
} else {
return permission;
}
}
refreshContacts() async {
PermissionStatus permissionStatus = await _getContactPermission();
if (permissionStatus == PermissionStatus.granted) {
// Load without thumbnails initially.
var contacts =
(await ContactsService.getContacts(withThumbnails: false)).toList();
// var contacts = (await ContactsService.getContactsForPhone("8554964652"))
// .toList();
setState(() {
_contacts = contacts;
});
// Lazy load thumbnails after rendering initial contacts.
for (final contact in contacts) {
ContactsService.getAvatar(contact).then((avatar) {
if (avatar == null) return; // Don't redraw if no change.
setState(() => contact.avatar = avatar);
});
}
} else {
_handleInvalidPermissions(permissionStatus);
}
}
updateContact() async {
Contact ninja = _contacts
.toList()
.firstWhere((contact) => contact.familyName.startsWith("Saifu"));
ninja.avatar = null;
await ContactsService.updateContact(ninja);
refreshContacts();
}
void _handleInvalidPermissions(PermissionStatus permissionStatus) {
if (permissionStatus == PermissionStatus.denied) {
throw new PlatformException(
code: "PERMISSION_DENIED",
message: "Access to location data denied",
details: null);
} else if (permissionStatus == PermissionStatus.denied) {
throw new PlatformException(
code: "PERMISSION_DISABLED",
message: "Location data is not available on device",
details: null);
}
}
@override
Widget build(BuildContext context) {
double width = MediaQuery.of(context).size.width;
double height = MediaQuery.of(context).size.height;
return Scaffold(
appBar: AppBar(title: Text('Phone Book'),),
body: _contacts != null
? Container(
height: height,
width: width,
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [
Color(0xff0D2C4F),
Color(0xff0D2C4F),
Color(0xff1D2631),
Color(0xff1D2631),
])),
child: SafeArea(
child: ListView.separated(
separatorBuilder: (context, index) {
return Divider(
height: height * 0.01,
color: Colors.white54,
);
},
itemCount: _contacts?.length ?? 0,
itemBuilder: (BuildContext context, int index) {
Contact c = _contacts?.elementAt(index);
return ListTile(
trailing: FlatButton(
shape: CircleBorder(),
color: Colors.red,
onPressed: (){
print(c.phones);
},
child: Icon(Icons.add, size: height * 0.025, color: Colors.white,),
),
leading: CircleAvatar(
child: Text('${c.displayName[0]}'),
radius: 15,
),
title: Text(
c.displayName ?? "",
style: TextStyle(color: Colors.white),
),
);
},
),
),
)
: Center(
child: CircularProgressIndicator(
valueColor: new AlwaysStoppedAnimation<Color>(Color(0xff1D2631)),
),
),
);
}
}
в собственности onPressed : () { Я напечатал номер телефона }
Любая помощь будет принята с благодарностью.