class HomePage extends StatelessWidget
{
IsAdminController isadmincont =
Get.find<IsAdminController>();
@override
void initState() {
isadmincont.setIsadmin();
}
bool doneload = false;
CollectionReference usersref =
FirebaseFirestore.instance.collection('users');
var receiverid;
CollectionReference adminsref =
FirebaseFirestore.instance.collection('admins');
late Admin admin;
Future<String> caller(partic) async {
var result = await getSenderName(partic);
return result;
}
//function to get OTHER name
Future<String> getSenderName(partic) async {
var otherid;
for (int x = 0; x < 2; x++) {
if (partic[x] != await FirebaseAuth.instance.currentUser?.uid) {
print('your partic ids are ${partic[x]}');
otherid = partic[x];
print('your uid is ${FirebaseAuth.instance.currentUser?.uid}');
}
}
print('going down');
var fname =
await FirebaseFirestore.instance.collection('users').doc(otherid).get();
if (fname.exists) {
print('this is a user');
return 'user';
}
if (!fname.exists) {
var fname = await FirebaseFirestore.instance
.collection('admins')
.doc(otherid)
.get();
print('is an admin with value ${fname['fname']}');
doneload = true;
return fname['fname'];
} else
return 'empty';
}
ConversationController convcont = Get.find<ConversationController>();
@override
Widget build(BuildContext context) {
return StreamBuilder(
stream: FirebaseFirestore.instance
.collection('messages')
.where("participants",
arrayContains: FirebaseAuth.instance.currentUser?.uid)
.snapshots(),
builder: (context, snapshot) {
if (snapshot.data != null) {
return ListView.builder(
itemCount: snapshot.data?.docs.length,
itemBuilder: (BuildContext context, int index) {
var lastmessage =
snapshot.data!.docs[index].get('lastmessage');
var senderid = snapshot.data!.docs[index].get('senderid');
var sendername = snapshot.data!.docs[index].get('sendername');
receiverid = snapshot.data!.docs[index].get('receiverid');
List<dynamic> partic =
snapshot.data!.docs[index].get('participants');
var userdisplayview =
snapshot.data!.docs[index].get('titleforuserview');
var admindisplayview =
snapshot.data!.docs[index].get('titleforadminview');
var conversation_title = '';
String convtitle = convcont._getConvTitle(partic).toString();
return ChatListItem(lastmessage, convtitle, senderid);
});
} else
return Text('');
});
}
Future<String> _getUserName(partid) async {
DocumentSnapshot userSnapshot =
await FirebaseFirestore.instance.collection('users').doc(partid).get();
DocumentSnapshot adminSnapshot =
await FirebaseFirestore.instance.collection('').doc(partid).get();
if (userSnapshot.exists) {
return userSnapshot['name'];
}
return userSnapshot['name'];
}
}
class ConversationController extends GetxController {
RxString conversationTitle = ''.obs;
Future<String> _getUserName(myparticlist, String userId) async {
RxList particlist = [].obs;
particlist.value = myparticlist;
particlist.forEach((participantId) async {
if (participantId != userId) {
String userName = await _getConvTitle(participantId);
conversationTitle.value = userName;
}
});
DocumentSnapshot userSnapshot =
await FirebaseFirestore.instance.collection('users').doc(userId).get();
return userSnapshot['name'];
}
Future<String> _getConvTitle(partid) async {
DocumentSnapshot userSnapshot =
await FirebaseFirestore.instance.collection('users').doc(partid).get();
DocumentSnapshot adminSnapshot =
await FirebaseFirestore.instance.collection('admins').doc(partid).get();
if (userSnapshot.exists) {
return userSnapshot['fname'];
} else
return userSnapshot['fname'];
}
}
i am coding a chat app using flutter and firestore and the chat function is working fine now.However I have run into an issue where If user x logs into the chat app, the all chats screen must display all the chats user x is involved in like WhatsApp where conversations have the name of the other person that a user is chatting with such that in the other person’s chat app they see user x’s name as the title of the conversation.The all chats screen musthave a list of conversations user x is involved in with each conversation having the title being the name of the other user they are chatting with such as usery,userb,userg, jonedoe,janedoe etc..how can i pull this off?
I have only managed to display conversations and show the last message per conversation
Seems like you have problem of designing flow. you can draw it first before you coding.
First you have node of Users, then you can have their ID, after that when they start chatting, you can add them to another node( could be Chat Group Node), and then create one more node for list of their conversation.
In case you have big user data, you can use Firebase Index to make query for user faster.
That is just my idea, you can define more simpler flow.