add these to android manifest file:
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
/main.dart initializing the contacts and asking for permissions
.
You can ask for the permissions wherever you like, it is just for the demonstration purposes.
void main() async {
WidgetsFlutterBinding.ensureInitialized();
// initializing contacts_provider
final contacts = Contacts();
await contacts.handlePermissions();
await contacts.init();
runApp(const MainApp());
}
/home.dart
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return ContactsBuilder(
onCreate: (event) {
// on create behavior goes here
// you can access the event from here.
// new contact list after event has happened
event.contactList;
// only effected contact list, in case of create
// it will be the contacts that were created
event.effectedContacts;
// event type
event.event;
},
onUpdate: (event) {
// on update behavior goes here
},
onDelete: (event) {
// on delete behavior goes here
},
onChange: () {
// If you specify this, it will be executed on any changes will happen in contacts;
},
builder: (context, allLatestContacts) {
// receive new list of contacts here.
return ListView.builder(
itemCount: allLatestContacts,
itemBuilder: (BuildContext context, int index) {
final contact = allLatestContacts[index];
return ContactListTile(contact: contact);
},
);
},
);
}
}
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Contacts.handlePermissions();
runApp(const MainApp());
}
class MainApp extends StatelessWidget {
const MainApp({super.key});
@override
Widget build(BuildContext context) {
return ContactsProvider(
child: MaterialApp(
home: Scaffold(
body: Center(
child: HomePage(),
),
),
),
);
}
}