fetchNext method

Future<List<BaseMessage>> fetchNext({
  1. required dynamic onSuccess(
    1. List<BaseMessage> message
    )?,
  2. required dynamic onError(
    1. CometChatException excep
    )?,
})

Returns a list of BaseMessage object fetched after putting the filters.

uid user with whom the conversation is to be fetched.

guid group for which the conversations are to be fetched.

searchTerm fetch messages that have search term.

afterMessageId provides messages only before the message-id.

limit informs the SDK to fetch the specified number of messages in one iteration.

The method could throw PlatformException with error codes specifying the cause

See also Android Documentation IOS Documentation

Implementation

Future<List<BaseMessage>> fetchNext({required Function(List<BaseMessage> message)? onSuccess, required Function(CometChatException excep)? onError}) async {
  try{
    int? updateAfterLong;
    int? timestampLong;
    if (this.updatedAfter != null) {
      updateAfterLong = this.updatedAfter!.millisecondsSinceEpoch ~/ 1000;
    }
    if (this.timestamp != null) {
      timestampLong = this.timestamp!.millisecondsSinceEpoch ~/ 1000;
    }
    final result = await channel.invokeMethod('fetchNextMessages', {
      'uid': this.uid,
      'guid': this.guid,
      'searchTerm': this.searchKeyword,
      'messageId': this.messageId,
      'limit': this.limit,
      'timestamp': timestampLong,
      'unread': this.unread,
      'hideblockedUsers': this.hideMessagesFromBlockedUsers,
      'updateAfter': updateAfterLong,
      'updatesOnly': this.updatesOnly,
      'categories': this.categories,
      'types': this.types,
      'parentMessageId': this.parentMessageId,
      'hideReplies': this.hideReplies,
      'hideDeletedMessages': this.hideDeleted,
      'withTags': this.withTags,
      'tags': this.tags,
      'key': this.key
    });
    final List<BaseMessage> res = [];
    if (result != null) {
      key = result["key"];
      if (result["list"] != null) {
        for (var _obj in result["list"]) {
          try {
            res.add(BaseMessage.fromMap(_obj));
          } catch (e) {
            if(onError != null) onError(CometChatException(ErrorCode.errorUnhandledException, e.toString() , e.toString()));
            return [];
          }
        }
      }
    }
    if(onSuccess != null) onSuccess(res);
    return res;
  } on PlatformException catch (platformException) {
    if(onError != null) onError(CometChatException(platformException.code, platformException.details, platformException.message));
  } catch (e) {
    debugPrint("Error: $e");
    if(onError != null) onError(CometChatException(ErrorCode.errorUnhandledException, e.toString() , e.toString()));
  }
  return [];
}