I want to add or remove members in a group chat app. For that I have a list of users. Here is my code:
QuerySnapshot<Map<String, dynamic>>? searchResultSnapshot;
QuerySnapshot<Map<String, dynamic>>? Snapshot;
bool isLoading = false;
bool haveUserSearched = false;
bool isChecked = false;
DatabaseMethods databaseMethods = new DatabaseMethods();
var searchEditingController = TextEditingController();
InitiateSearch() async {
if (searchEditingController.text.isNotEmpty) {
setState(() {
isLoading = true;
});
await databaseMethods.GetUserByUsername(searchEditingController.text)
.then((snapshot) {
searchResultSnapshot = snapshot;
print("$searchResultSnapshot");
setState(() {
isLoading = false;
haveUserSearched = true;
});
});
}
}
GetUsersList() async {
if (searchEditingController.text.isEmpty) {
setState(() {
isLoading = true;
});
await databaseMethods.GetUsers().then((snapShot) {
Snapshot = snapShot;
setState(() {
isLoading = false;
haveUserSearched = false;
});
});
}
}
addremovemember(userName){
print('caa');
final docRef = FirebaseFirestore.instance.collection("groups").doc(widget.groupRoomId);
docRef.snapshots().listen((datasnapshot){
if (datasnapshot.data()!.containsValue("$userName")){
FirebaseFirestore.instance
.collection('groups')
.doc(widget.groupRoomId)
.update({"users" : FieldValue.arrayRemove([userName])});
}
else{
FirebaseFirestore.instance
.collection('groups')
.doc(widget.groupRoomId)
.update({"users" : FieldValue.arrayUnion([userName])});
}
});
}
Widget UserList() {
return Expanded(
child: isLoading
? Container(
child: Center(
child: CircularProgressIndicator(),
),
)
: haveUserSearched
? ListView.builder(
itemCount: searchResultSnapshot?.docs.length,
itemBuilder: (context, index) {
return Listtile(
searchResultSnapshot?.docs[index].data()["userName"],
searchResultSnapshot?.docs[index].data()["userEmail"],
);
})
: ListView.builder(
itemCount: Snapshot?.docs.length,
itemBuilder: (context, index) {
return Listtile(
Snapshot?.docs[index].data()["userName"],
Snapshot?.docs[index].data()["userEmail"],
);
}),
);
}
Widget Listtile(String? userName, String? userEmail) {
return ListTile(
title: Text(userName ?? "",
style: TextStyle(color: Colors.white, fontSize: 16)),
subtitle: Text(
userEmail ?? "",
style: TextStyle(color: Colors.white, fontSize: 16),
),
onTap: () => (){addremovemember(userName); print("FHFGFH");},
);
}
Now when I tap list-tile it is not printing any thing. I think it means that there is some problem with onTap method and that is why addremovemember( ) is never called. I don't know where the main problem lies. Please help me out with this.