query method
Future<ChannelState>
query({
- bool state = true,
- bool watch = false,
- bool presence = false,
- PaginationParams? messagesPagination,
- PaginationParams? membersPagination,
- PaginationParams? watchersPagination,
- bool preferOffline = false,
Query the API, get messages, members or other channel fields.
Set preferOffline
to true to avoid the API call if the data is already
in the offline storage.
Implementation
Future<ChannelState> query({
bool state = true,
bool watch = false,
bool presence = false,
PaginationParams? messagesPagination,
PaginationParams? membersPagination,
PaginationParams? watchersPagination,
bool preferOffline = false,
}) async {
ChannelState? channelState;
try {
// If we prefer offline, we first try to get the channel state from the
// offline storage.
if (preferOffline && !watch && cid != null) {
final persistenceClient = _client.chatPersistenceClient;
if (persistenceClient != null) {
final cachedState = await persistenceClient.getChannelStateByCid(
cid!,
messagePagination: messagesPagination,
);
// If the cached state contains messages, we can use it.
if (cachedState.messages?.isNotEmpty == true) {
channelState = cachedState;
}
}
}
// If we still don't have the channelState, we try to get it from the API.
channelState ??= await _client.queryChannel(
type,
channelId: id,
channelData: _extraData,
state: state,
watch: watch,
presence: presence,
messagesPagination: messagesPagination,
membersPagination: membersPagination,
watchersPagination: watchersPagination,
);
if (_id == null) {
_id = channelState.channel!.id;
_cid = channelState.channel!.cid;
}
// Initialize the channel state if it's not initialized yet.
if (this.state == null) {
_initState(channelState);
} else {
// Otherwise, update the channel state.
this.state?.updateChannelState(channelState);
}
return channelState;
} catch (e, stk) {
// If we failed to get the channel state from the API and we were not
// supposed to watch the channel, we will try to get the channel state
// from the offline storage.
if (watch == false) {
if (_client.persistenceEnabled) {
return _client.chatPersistenceClient!.getChannelStateByCid(
cid!,
messagePagination: messagesPagination,
);
}
}
// Otherwise, we will just rethrow the error.
if (!_initializedCompleter.isCompleted) {
_initializedCompleter.completeError(e, stk);
}
rethrow;
}
}