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.

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,
      'myMentionsOnly': this._myMentionsOnly ?? false,
      'mentionsWithTagInfo': this._mentionsWithTagInfo ?? false,
      'mentionsWithBlockedInfo': this._mentionsWithBlockedInfo ?? false,
      'interactionGoalCompletedOnly': this.interactionGoalCompletedOnly
    });
    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 [];
}