contacts_service: ^0.6.3 을 입력dependencies:
flutter:
sdk: flutter
permission_handler: ^8.3.0
contacts_service: ^0.6.3
import 'package:contacts_service/contacts_service.dart'; 를 main.dart 파일 가장 상단에 추가var contacts = await ContactsService.getContacts();var contacts = await ContactsService.getContacts(withThumbnails: false);var contacts = await ContactsService.getContacts(withThumbnails: false);
print(contacts[0].givenName)new 키워드 생략 가능
.givenName → 이름
.familyName → 성
var newContact = new Contact();
newContact.givenName = '민수';
await ContactsService.addContact(newContact);
(getPermission 함수 안쪽)
var contacts = await ContactsService.getContacts(withThumbnails: false);
print(contacts[0].givenName)
setState(() {
name = contacts;
}); → 에러 발생 : A value of type 'List' can't be assigned to a variable of type 'List'name 변수를 만들 때 리스트를 비워두어 dynamic 타입이 되도록 하기
var name = ['영희', '횟집', '미용실']; 이거를
var name = []; 이걸로 고쳤습니다
body: ListView.builder(
itemCount: name.length,
itemBuilder: (c, i) {
return ListTile(
leading: Icon(Icons.account_circle_rounded),
title: Text(name[i].givenName),
);
},
)Contact라는 자료가 들어오는 name 변수로 만들기
List<Contact> name = [];
타입 캐스팅 사용
Union type 사용
import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:contacts_service/contacts_service.dart';
void main() {
runApp(
MaterialApp(
home: MyApp()
)
);
}
class MyApp extends StatefulWidget {
MyApp({Key? key}) : super(key: key);
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
getPermission() async {
var status = await Permission.contacts.status;
if (status.isGranted) { // 허락된 경우
print('허락됨');
var contacts = await ContactsService.getContacts();
// print(contacts[0].givenName);
setState(() {
name = contacts;
});
} else if (status.isDenied) { // 거절된 경우
print('거절됨');
Permission.contacts.request(); // 현재 거절된 상태니 팝업창 띄워달라는 코드
openAppSettings();
}
}
void initState() {
// TODO: implement initState
super.initState();
getPermission();
}
var total = 3;
var like = [0, 0, 0];
var name = [];
addName(a) {
setState(() {
name.add(a);
});
}
addOne() {
setState(() {
total++;
});
}
Widget build(BuildContext context) {
return Scaffold(
floatingActionButton: FloatingActionButton(
onPressed: () {
showDialog(context: context, builder: (context) {
return DialogUI(addOne: addOne, addName : addName );
});
},
),
appBar: AppBar( title: Text(total.toString()), actions: [
IconButton(onPressed: () { getPermission();}, icon: Icon(Icons.contacts))
],),
body: ListView.builder(
itemCount: name.length,
itemBuilder: (c, i) {
return ListTile(
leading: Icon(Icons.account_circle_rounded),
title: Text(name[i].givenName),
);
},
)
);
}
}
class DialogUI extends StatelessWidget {
DialogUI({Key? key, this.addOne, this.addName}) : super(key: key);
final addOne, addName;
var inputData = TextEditingController();
var inputData2 = '';
Widget build(BuildContext context) {
return Dialog(
child: SizedBox(
width: 300,
height: 300,
child: Column(
children: [
TextField(controller: inputData),
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
TextButton(child: Text('취소'), onPressed: (){ Navigator.pop(context);}),
TextButton(
child: Text('완료'),
onPressed: () {
addOne();
var newContact = Contact();
newContact.givenName = inputData.text; //새로운 연락처 만들기
ContactsService.addContact(newContact); //실제로 연락처에 집어넣기
addName(newContact);
})
],
)
],
),
)
);
}
}