我的代码有效,但是,我担心的是,我参与了很多写操作,并且可能进行太多的查询,而这些查询可以用更简单的解决方案来处理。
firestore数据库结构如下。
组(集合)/组文档/成员(子集合)/具有与该组关联的用户文档匹配的ID的文档
我的代码执行以下操作 1.获得访问联系人的权限 2.从用户设备中提取联系人 3.从我的平台中为与之关联的所有组提取联系人 4.删除重复的名称 5.将设备和平台上的联系人合并在一起,并按字母顺序显示在switchTile中,以供用户选择要添加到新组的联系人。
当前,我首先拉出当前登录用户所关联的所有组。
然后,我循环浏览这些组中的每个组,并获取与这些组关联的所有成员ID,并将它们放置在数组中并删除重复的IDS。
在消除所有重复的IDS之后,我从User集合中提取联系人文档,并创建一个数组以将设备联系人与这些已被拉出的平台联系人合并。
是否有一种更简单的方法来完成所有这一个或两个查询
List <Contact> contactList;
List sortedCompleteList;
final Firestore db = Firestore.instance;
final FirebaseAuth _auth = FirebaseAuth.instance;
final CollectionReference groupsRef = Firestore.instance.collection('groups');
final CollectionReference usersRef = Firestore.instance.collection('users');
List memberIdsNoduplicates = [];
List<User> contactUsers = [];
bool isLoading = false;
bool contactsLoaded = false;
getBubbles() async{
QuerySnapshot groups = await usersRef.document(Provider.of<UserData>(context).getUserId).collection('groups').getDocuments(); //returns private bubbles of user.
for (DocumentSnapshot doc in groups.documents){
QuerySnapshot membersIds = await groupsRef.document(doc.documentID).collection('members').getDocuments(); //returns Ids of members
for (DocumentSnapshot memberDoc in membersIds.documents){
memberIdsNoduplicates.add(memberDoc.documentID); //adds each member id to memberIdsNoDuplicates array
}
}
}
removeDuplicate(List arr){
Map<dynamic, int> duplicateMap = {};
for (dynamic ele in arr){
duplicateMap[ele] = 0;
}
return duplicateMap.keys.toList();
}
gettingDistinctMembers() async {
List nonDuplicated = await removeDuplicate(memberIdsNoduplicates); //removes duplicate contacts
for (String nonDupId in nonDuplicated){
DocumentSnapshot userDoc = await usersRef.document(nonDupId).get(); //pulls contact info
User friendUser = User.fromDocument(userDoc); //puts contact info in user object form
print("The Name of the user is ${friendUser.name}");
contactUsers.add(friendUser);
}
}
getContacts() async{
Iterable <Contact> contacts = await ContactsService.getContacts(withThumbnails: true); //gets contacts if permission is granted
print(contacts);
contactList = contacts.toList();
print(contactList);
print(contacts);
setState(() {
isLoading = true;
});
await getBubbles(); //gets contacts of users in bubbles
await gettingDistinctMembers(); //gets distinct members without duplicates
List completeList = [...contactList, ...contactUsers];
sortedCompleteList = completeList.map((mixedContact){
if (mixedContact is User){
return {
"name": mixedContact.name,
"contact": mixedContact,
"selected": false,
};
} else if (mixedContact is Contact) {
return {
"name": mixedContact.displayName,
"contact": mixedContact,
"selected": false
};
}
}).toList();
sortedCompleteList.sort((a,b) => a['name'].compareTo(b['name']));
setState(() {
isLoading = false;
contactsLoaded = true;
});
}
contactsDisplayed(){
return ListView.builder(itemCount: sortedCompleteList.length, itemBuilder: (context, index){
return Card(
child: SwitchListTile(secondary: CircleAvatar(
backgroundImage: sortedCompleteList[index]['contact'] is User ? NetworkImage(sortedCompleteList[index]['contact'].mediaUrl):
MemoryImage(sortedCompleteList[index]['contact'].avatar),
),
title: Text(sortedCompleteList[index]['name']),
value: sortedCompleteList[index]['selected'],),
);
});
}
@override
void initState() {
checkPermission();
super.initState();
}
void checkPermission() async{
permission = await PermissionHandler().checkPermissionStatus(PermissionGroup.contacts);
print(permission);
switch (permission){
case PermissionStatus.granted : { //if permission is granted then we get contacts
getContacts();
break;
}
case PermissionStatus.denied : {
Map<PermissionGroup, PermissionStatus> permissions = await PermissionHandler().requestPermissions([PermissionGroup.contacts]); //request permission
if (permissions[PermissionGroup.contacts] == PermissionStatus.granted) //checks map to see if permission on contacts was granted
getContacts();
break;
}
case PermissionStatus.disabled : {
bool isOpened = await PermissionHandler().openAppSettings();
break;
}
case PermissionStatus.restricted : {
try {
getContacts();
}catch(e){
print(e);
}
break;
}
case PermissionStatus.unknown : {
Map<PermissionGroup, PermissionStatus> permissions = await PermissionHandler().requestPermissions([PermissionGroup.contacts]);
}
}````