Here is my situation:
I have a chat app, which has a ListView
of all the chats on the main screen. Each ListTile
has widgets such as unread icons, chat title, etc. Currently, I have set it up so that these small widgets have their own query watcher to check when any data has changed and re-render accordingly.
However, one thing I have noticed is that even though my query watcher is pointed for a specific chat, it still executes if another completely unrelated chat is changed. This is some example code of what I mean:
final unreadQuery = chatBox.query(Chat_.guid.equals(controller.chat.guid)).watch();
sub = unreadQuery.listen((Query<Chat> query) {
final chat = query.findFirst()!;
if (chat.hasUnreadMessage != unread) {
setState(() {
unread = chat.hasUnreadMessage!;
});
}
});
So, even though the query is specifically where Chat_.guid.equals(controller.chat.guid)
, a completely unrelated chat update will still call the stream. This means that if one chat is unread, it will call the query findFirst()
for the hundreds+ ListTile
s that are in the view, even though their read status did not change. Is this bad for performance? Basically it seems like it would be spamming the database with hundreds of calls that are not needed.
Or is there a better way of doing this? One way I was thinking is to have a one singular query watcher, watching all the chats, and then have these widgets "subscribe" into this watcher with custom functions to filter out the data that they don't care about. Something like this:
chatBox.query().watch().listen((event) {
final chats = event.find();
for (Function f in _watcherFunctions) {
f.call(chats);
}
});
Here, the query will be called only once for any update for any chat, rather than a query called hundreds of times, however this time it is pulling in a lot more data because instead of fetching one chat it is fetching hundreds in one go. I am not sure which way is the best route to go, and would greatly appreciate any guidance. Thanks!