Ive got a simple chat feature in my app, only text. Right now Im using a StreamProvider utilizing the Provider package. Like this:
return MultiProvider(
providers: [
FutureProvider.value(
value: DatabaseProvider().getDMProfile(pairing.dmId)),
FutureProvider.value(
value: DatabaseProvider().getPlayerProfile(pairing.playerId)),
StreamProvider.value(value: ChatProvider().streamMessages(chatName)),
],
child: ChatScreen(
hidden: isPlayer ? pairing.playerHidden : pairing.dmHidden,
isPlayer: isPlayer,
chatName: chatName,
),
);
Heres the code for the stream:
Stream<List<Message>> streamMessages(String chatId) {
var ref = db
.collection(CHAT_COLLECTION)
.document(chatId)
.collection(MESSAGES_COLLECTION)
.orderBy('timestamp', descending: true);
return ref.snapshots().map((list) =>
list.documents.map((doc) => Message.fromMap(doc.data)).toList());
}
This all works fine as I create a ListView to display the chats. What Im wondering is if pulling all the messages like that is the right way to do it for efficiency. And if not, how can I add in pagination so as the user scrolls up it loads additional past messages.