loadMoreMessages method

Future<void> loadMoreMessages()

to load more messages call this function

Implementation

Future<void> loadMoreMessages() async {
  await Future.delayed(const Duration(seconds: 1));
  if (offlineMessages.isNotEmpty) {
    customPrint("datadatadatadatadat:::::${offlineMessages.last.message}");
    QuerySnapshot snap = await chatCollectionReference
        .doc(chatId)
        .collection('messages')
        // .where("visibleTo", arrayContains: currentUserId)
        .where("createdAt", isLessThan: offlineMessages.last.createdAt)
        .orderBy('createdAt', descending: false)
        .limit(10)
        .get();
    if (snap.docs.isNotEmpty) {
      List<QueryDocumentSnapshot<Object?>> sn = snap.docs.reversed.toList();
      List.generate(sn.length, (i) {
        Map<dynamic, dynamic> d = sn[i].data() as Map;
        Timestamp t = d['createdAt'];
        d['createdAt'] = t.toDate().toString();
        newData[sn[i].id] = d;
        messageData[sn[i].id] =
            ChatModal.fromJson(d, sn[i].id, isCachedTemp: true);
        offlineMessages.add(ChatModal.fromJson(d, sn[i].id, isCachedTemp: true));
      });
      Map<String, dynamic> sortedMap = ChatServices.sortMapByCreatedAt(newData);
      if (isNormalChatSavedLocally)
        await HS.chatFilesBox.put(
            chatId,
            jsonEncode(sortedMap));
      test.value = !test.value;
    } else {
      isLastPage = true;
      test.value = !test.value;
    }
  } else {
    isLastPage = true;
    test.value = !test.value;
  }
}