How is this done with Realtime Database? My app reads and writes to Realtime Database for messages in a group chat. I need to show the user which group has new activity, so in order to do that I need to differentiate between messages that the user has already read and new messages.
My app is fundamentally using onValue.listen
to listen to a node, adding the messages to a List
, passing them through a model, then displaying them using a ListView.builder
. Is this a good setup to somehow add the ability to know which messages are new and which have already been read? If not, what type of setup do I need?
void getChatListener() async {
final node = globals.groupChatsNode(groupID: widget.groupID);
_groupChatRef = FirebaseDatabase.instance.ref(node);
_chatsSubscription = _groupChatRef.onValue.listen((DatabaseEvent event) {
dataList = [];
for (final child in event.snapshot.children) {
dataList.add(child.value);
}
setState(() {
chats = dataList;
});
});
}
@override
Widget build(BuildContext context) {
return ListView.builder(
itemCount: chats.length,
itemBuilder: (context, index) {
final msg = ModelChatData.fromJson(chats[index]);
//...display the message
}
)
}
Edit: My question is different from the other questions marked as similar. First, the one question that uses Firestore isn't related at all because I'm asking about Realtime Database, not Firestore. Second, the other question (which actually uses Realtime Database) says how to store the seenBy
value in the database and how to display it, but it doesn't cover how to know when a single user has seen a single message. Third, my question is not about counting how many people have read a message, it is about just knowing if the current user has read a message or not.
Please reopen this question, given that the linked questions do not answer this question.