Get channels
The client SDK generates channels in the local database based on sent and received messages. Use query objects to paginate through the channel list, and use identifier-based lookups when you already know which channels you need.
Use the callback to read the final result. For query objects such as ChannelsQuery, pagination results come from loadNextPage().
Get the channel list
Retrieve channels from the local database with pagination. Channels are sorted by operation time, and pinned channels can be returned first.
Method
static ChannelsQuery createChannelsQuery(ChannelsQueryParams params)
ChannelsQueryParams
| Parameter | Type | Default | Description |
|---|---|---|---|
channelTypes | List<ChannelType> | Required | Channel types to include in the query. |
startTime | int | 0 | Start timestamp in milliseconds. Use the operationTime of the last channel in the previous page to continue pagination. |
pageSize | int | 20 | Number of channels per page. Must be greater than 0 and no more than 50. |
topPriority | bool | true | Whether pinned channels should be returned before non-pinned channels. |
Code example
final query = BaseChannel.createChannelsQuery(
ChannelsQueryParams(
channelTypes: [ChannelType.direct, ChannelType.group, ChannelType.system],
startTime: 0,
pageSize: 20,
topPriority: true,
),
);
await query.loadNextPage((result, error) {
if (error == null) {
print('Channel count: ${result?.data.length}');
} else {
print('Failed, error code: ${error?.code}');
}
});
PageData<BaseChannel>
| Property | Type | Description |
|---|---|---|
data | List<BaseChannel> | Channels returned in the current page. |
Get specific channels by identifiers
Use BaseChannel.getChannels() when you already know the exact channels you want to read from the local database.
Method
static Future<int> getChannels(List<ChannelIdentifier> identifiers, OperationHandler<List<BaseChannel>> handler)
ChannelIdentifier properties
| Property | Type | Description |
|---|---|---|
channelType | ChannelType | Channel type. |
channelId | String | Channel ID. |
subChannelId | String? | Subchannel ID for community channels only. |
Code example
await BaseChannel.getChannels(
[
ChannelIdentifier(channelType: ChannelType.direct, channelId: 'user1'),
ChannelIdentifier(channelType: ChannelType.group, channelId: 'group1'),
],
(channels, error) {
if (error == null) {
print('Matched channels: ${channels?.length}');
}
},
);
Reload a channel
Reload the latest details for a specific channel from the local database. This is a BaseChannel instance method.
Method
Future<int> reload(OperationHandler<BaseChannel> handler)
Code example
BaseChannel channel = ...;
await channel.reload((updated, error) {
if (error == null) {
print('Unread count: ${updated?.unreadCount}');
}
});
Get channels with unread messages
Retrieve channels that have unread messages. Supports direct, group, and system channels.
Method
static Future<int> getUnreadChannels(List<ChannelType> channelTypes, OperationHandler<List<BaseChannel>> handler)
Code example
final channelTypes = [ChannelType.direct, ChannelType.group, ChannelType.system];
await BaseChannel.getUnreadChannels(channelTypes, (channels, error) {
if (error == null) {
print('Channels with unread messages: ${channels?.length}');
}
});
Search channels
Use BaseChannel.searchChannels() to search channels by keyword and message type.
Method
static Future<int> searchChannels(SearchChannelsParams params, OperationHandler<List<SearchChannelResult>> handler)
SearchChannelsParams
| Parameter | Type | Description |
|---|---|---|
channelTypes | List<ChannelType> | Channel types to search. |
messageTypes | List<MessageType> | Message types to include in the search. |
keyword | String | Search keyword. |
Code example
await BaseChannel.searchChannels(
SearchChannelsParams(
channelTypes: [ChannelType.direct, ChannelType.group],
messageTypes: [MessageType.text, MessageType.image],
keyword: 'release',
),
(results, error) {
if (error == null) {
print('Matched channels: ${results?.length}');
print('First count: ${results?.first.count}');
}
},
);
SearchChannelResult
| Property | Type | Description |
|---|---|---|
channel | BaseChannel? | The matched channel. |
count | int? | Number of matching messages in the channel. |
FAQ
Q: After reinstalling the app, the channel list is empty or partially missing?
A: The channel list is stored in the SDK's local database, which is cleared when the app is uninstalled. If missed message compensation is enabled, logging in on a new device triggers compensation to retrieve some historical channels (default: current day). To extend the compensation period (up to 7 days), submit a ticket. Setting a long period may cause processing pressure on clients with high message volumes.