AssetQuery
AssetQuery
Warning Contentstack has deprecated the Dart Delivery SDK for new projects. Existing apps can keep using it. For new Dart or Flutter projects, call the Content Delivery API directly with an HTTP client.
Assets are the media files uploaded to a stack, such as images, videos, PDFs and audio files. AssetQuery lists the published assets of a stack. Use this class for the following tasks:
- List the assets published in an environment, one page at a time.
- Page and sort the list with limit, skip and the two order methods.
- Add the total count, image dimensions or branch to the response.
stack.assetQuery() takes no arguments and returns an AssetQuery. To read one asset by its UID, call stack.asset('<ASSET_UID>'), which returns an Asset.
AssetQuery inherits nine methods from BaseQuery, the class that Query and ContentTypeQuery also inherit from. Six of them change the asset request. The three filter methods do not, as the Class-Level Notes below describe.
Class-Level Notes
Importing the type. The main library, package:contentstack/contentstack.dart, does not export AssetQuery. Declare the variable with final, as the examples do, or import the type from its source file when your code names it:
import 'package:contentstack/src/asset_query.dart';
What find sends.
- One find call sends one Get all assets request, GET /v3/assets. The version segment comes from the apiVersion you pass to Stack, 'v3' by default.
- find sends every top-level query parameter that the setters store, and the api_key, access_token and environment headers from the Stack.
- find does not send the field conditions that the where, addQuery and query methods store. Each of those pages gives the workaround in a Known issue line.
Pagination.
- One find call returns one page. The SDK does not request further pages.
- The API returns the first 100 assets when the request sets no limit. See Pagination.
- To read further pages, set limit and skip, and call includeCount to learn the total.
Environment. stack.assetQuery() copies the stack's environment into the environment query parameter. The environment method replaces that query parameter for one query. The environment header keeps the stack's environment.
Branch scoping. When you pass branch to the Stack constructor, find sends it as the branch header, so the request lists the assets of that branch.
Settings stay on the instance.
- Each setter writes under a fixed query parameter key, and a second call on the same key replaces the first value.
- No method removes a stored parameter. To send a request without one, create a new query with stack.assetQuery().
Capability Matrix
| Capability | Supported | Notes / Alternative |
|---|---|---|
| Filter by field value with where, addQuery or query | No | Send the condition with param('query', jsonEncode(...)), or filter the returned list in your app |
| Read every asset in one call | No | Call find once per page with limit and skip |
| Read one asset by UID | No | Use the fetch method of Asset |
| Typed list with find<List<AssetModel>, AssetModel>() | No | Call find() and build each model with AssetModel.fromJson |
Method Index
Every method below is available on stack.assetQuery(). The groups follow what each method does to the request.
List assets. Available on stack.assetQuery() as the last call of a chain.
| Method Name | Returns | Description |
|---|---|---|
| find | The matching assets. | Sends the request and returns one page of assets. |
Shape the response. Available on stack.assetQuery().
| Method Name | Returns | Description |
|---|---|---|
| includeCount | Nothing. The query keeps the setting for its next request. | Adds the total number of matching assets. |
| includeDimension | Nothing. The query keeps the setting for its next request. | Adds the height and width of each image. |
| includeFallback | Nothing. The query keeps the setting for its next request. | Requests fallback-locale details when a localization is missing. |
| includeBranch | Nothing. The query keeps the setting for its next request. | Adds the branch of each asset. |
| relativeUrls | Nothing. The query keeps the setting for its next request. | Sets the relative_urls query parameter. |
| version | Nothing. The query keeps the setting for its next request. | Sets the version query parameter. |
| environment | Nothing. The query keeps the setting for its next request. | Sets the environment query parameter. |
Page and sort the list. Available on stack.assetQuery(). AssetQuery inherits these methods from BaseQuery.
| Method Name | Returns | Description |
|---|---|---|
| limit | Nothing. The query keeps the setting for its next request. | Caps the number of assets on the page. |
| skip | Nothing. The query keeps the setting for its next request. | Skips a number of assets before the page starts. |
| orderByAscending | Nothing. The query keeps the setting for its next request. | Sorts the assets in ascending order of a field. |
| orderByDescending | Nothing. The query keeps the setting for its next request. | Sorts the assets in descending order of a field. |
Add query parameters. Available on stack.assetQuery(). AssetQuery inherits these methods from BaseQuery.
| Method Name | Returns | Description |
|---|---|---|
| param | Nothing. The query keeps the setting for its next request. | Adds one query parameter. |
| addParams | Nothing. The query keeps the setting for its next request. | Adds several query parameters. |
Inherited filters with no effect. Available on stack.assetQuery() through BaseQuery, but find does not send what they store.
| Method Name | Returns | Description |
|---|---|---|
| where | Nothing. The query keeps the setting for its next request. | Stores a field condition that find does not send. |
| addQuery | Nothing. The query keeps the setting for its next request. | Stores conditions that find does not send. |
| query | Nothing. The query keeps the setting for its next request. | Stores a field condition that find does not send. |
Class-Level Snippet
The snippet below lists the ten newest assets with their dimensions, and prints the total count and each title and URL.
import 'package:contentstack/contentstack.dart' as contentstack;
Future<void> main() async {
final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');
final query = stack.assetQuery()
..includeCount()
..includeDimension()
..orderByDescending('created_at')
..limit(10);
try {
final response = await query.find();
if (response is Map && response.containsKey('error_code')) {
// The API rejected the request
print('Request failed: ${response['error_code']} ${response['error_message']}');
} else {
print('Total assets: ${response['count']}');
for (final asset in response['assets']) {
print('${asset['title']} ${asset['url']}');
}
}
} catch (e) {
// Thrown for a non-JSON response, a network failure or a 30-second timeout
print('Request did not complete: $e');
}
}Methods
Each method of the AssetQuery class has its own section below, with its parameters, validation rules, behavior, and examples.
addParams
addParams adds several query parameters, from a map of names and values, to the request that find sends.
| Name | Type | Description |
|---|---|---|
| parameters (required) | Map | Query parameter names mapped to their values. |
Returns: void. Nothing. The query keeps the setting for its next request.
Validation
- An empty map changes nothing.
- Every key and value must be a String. Another type, such as {'limit': 10}, throws a TypeError at the call, because the query stores its parameters in a map of strings. Write {'limit': '10'} instead.
- addParams does not check the names. An unknown parameter surfaces when find() sends the request. The API rejection comes back as the result, not as an exception. Check the result for an error_code key.
Additional Resource Refer to Content Delivery API Errors for the full error code list.
Behavior
- Client-side only. addParams writes each entry into the query's top-level parameters and makes no request. The find() call that follows sends them.
- Each entry replaces any value already stored under the same key, including the values that other methods write, such as environment or limit.
- Unlike the param method, addParams keeps an entry whose value is an empty string, and sends that parameter with no value.
- The Get all assets reference lists parameters that AssetQuery has no method for, such as include_metadata and asset_fields[].
Example
Basic usage: request metadata and visual markups for each asset
import 'package:contentstack/contentstack.dart' as contentstack;
Future<void> main() async {
final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');
final query = stack.assetQuery()
..addParams({'include_metadata': 'true', 'asset_fields[]': 'visual_markups'});
try {
final response = await query.find();
if (response is Map && response.containsKey('error_code')) {
// The API rejected the request
print('Request failed: ${response['error_code']} ${response['error_message']}');
} else {
for (final asset in response['assets']) {
print('${asset['title']} ${asset['_metadata']} ${asset['visual_markups']}');
}
}
} catch (e) {
// Thrown for a non-JSON response, a network failure or a 30-second timeout
print('Request did not complete: $e');
}
}addQuery
addQuery stores several field conditions from a map, but find does not send them on an asset query.
| Name | Type | Description |
|---|---|---|
| parameters (required) | Map | Field UIDs mapped to their conditions. |
Returns: void. Nothing. The query keeps the setting for its next request.
Validation
- An empty map changes nothing.
- Every key must be a String. Another key type throws a TypeError at the call. Values can be any JSON value, such as a string, a number or a map with an operator.
- Because find does not send the conditions, addQuery cannot cause an API error on an asset query. Errors that do occur come from the find() call that follows. The API rejection comes back as the result, not as an exception. Check the result for an error_code key.
Known issue find does not send the conditions that addQuery stores, so the request returns every asset, unfiltered. Send the same map as JSON with the param method, param('query', jsonEncode({...})), or filter the returned list in your app.
Additional Resource Refer to Content Delivery API Errors for the full error code list.
Behavior
- Client-side only. addQuery writes each entry into the query's field conditions and makes no request.
- Each entry replaces a condition already stored on the same field, including one from the where or query method.
- On a Query for entries, find sends the stored conditions as the query parameter.
Example
Workaround: send the conditions with param instead of addQuery
import 'dart:convert';
import 'package:contentstack/contentstack.dart' as contentstack;
Future<void> main() async {
final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');
final conditions = {'title': 'Hero banner', 'filename': 'hero-banner.png'};
// addQuery stores the conditions, but find does not send them on an asset query
final ignored = stack.assetQuery()..addQuery(conditions);
// param sends the same conditions as the query parameter
final filtered = stack.assetQuery()..param('query', jsonEncode(conditions));
try {
final all = await ignored.find();
final matching = await filtered.find();
if (matching is Map && matching.containsKey('error_code')) {
// The API rejected the request
print('Request failed: ${matching['error_code']} ${matching['error_message']}');
} else {
print('Unfiltered: ${all['assets'].length}, matching: ${matching['assets'].length}');
}
} catch (e) {
// Thrown for a non-JSON response, a network failure or a 30-second timeout
print('Request did not complete: $e');
}
}environment
environment sets the environment query parameter that find sends for this query.
| Name | Type | Description |
|---|---|---|
| environment (required) | String | Publishing environment to list assets from. |
Returns: void. Nothing. The query keeps the setting for its next request.
Validation
- There is no client-side validation. environment stores any string, including an empty one.
- An invalid value surfaces when find() sends the request. The API rejection comes back as the result, not as an exception. Check the result for an error_code key.
Additional Resource Refer to Content Delivery API Errors for the full error code list.
Behavior
- Client-side only. environment replaces the environment query parameter that stack.assetQuery() copied from the Stack, and makes no request.
- The change applies to this query only. The environment header that the Stack sends on every request keeps the environment you passed to the Stack constructor.
- The API expects a delivery token that belongs to the environment you read from. See the access_token header in Get all assets. To list assets from a second environment, create a second Stack with that environment and its delivery token.
- An empty string sends the environment query parameter with no value.
Example
Basic usage: list the assets of a named environment
import 'package:contentstack/contentstack.dart' as contentstack;
Future<void> main() async {
final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');
final query = stack.assetQuery()..environment('<ENVIRONMENT>');
try {
final response = await query.find();
if (response is Map && response.containsKey('error_code')) {
// The API rejected the request
print('Request failed: ${response['error_code']} ${response['error_message']}');
} else {
for (final asset in response['assets']) {
print('${asset['title']} ${asset['publish_details']}');
}
}
} catch (e) {
// Thrown for a non-JSON response, a network failure or a 30-second timeout
print('Request did not complete: $e');
}
}find
find sends the asset query, with every setting the query holds, and returns one page of published assets.
This method takes no parameters. Its two type arguments, T and K, choose the result type. Pass none to get the response as a Map.
Returns: Future<T>. The matching assets.
Validation
- There is no client-side validation. find sends whatever query parameters the query holds.
- find throws only when the request does not complete: a response that is not JSON, a network failure, or a TimeoutException after 30 seconds.
- An API rejection comes back as the result, not as an exception. The untyped result is a Map with error_code and error_message keys.
- Check the result for an error_code key even inside a try block.
Known issue find<List<contentstack.AssetModel>, contentstack.AssetModel>() throws a TypeError, both on success and on an API rejection. Call find() without type arguments and build each model with contentstack.AssetModel.fromJson, as the second example below does.
Additional Resource Refer to Content Delivery API Errors for the full error code list.
Behavior
- One call sends one Get all assets request, GET /v3/assets, with every query parameter the setters stored and the headers of the Stack.
- find returns the decoded JSON body as a Map. Read the list from response['assets'], and the total from response['count'] after you call includeCount.
- find does not send the conditions that the where, addQuery and query methods store, so a chain with only those filters returns every asset.
- find returns one page. The API returns the first 100 assets when the request sets no limit. See Pagination.
- Calling find twice sends two requests, because the SDK caches nothing.
Limitations
- Does not read more than one page per call. Call find once per page with limit and skip, as the last example below does.
Example
Basic usage: list the assets in an environment
import 'package:contentstack/contentstack.dart' as contentstack;
Future<void> main() async {
final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');
final query = stack.assetQuery();
try {
final response = await query.find();
if (response is Map && response.containsKey('error_code')) {
// The API rejected the request
print('Request failed: ${response['error_code']} ${response['error_message']}');
} else {
for (final asset in response['assets']) {
print('${asset['uid']} ${asset['title']}');
}
}
} catch (e) {
// Thrown for a non-JSON response, a network failure or a 30-second timeout
print('Request did not complete: $e');
}
}Typed result: build an AssetModel for each asset
import 'package:contentstack/contentstack.dart' as contentstack;
Future<void> main() async {
final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');
final response = await stack.assetQuery().find();
final models = (response['assets'] as List)
.map((item) => contentstack.AssetModel.fromJson(item as Map<String, dynamic>))
.toList();
for (final model in models) {
print('${model.title} ${model.fileSize}');
}
}Edge case: read every asset, one page at a time
import 'package:contentstack/contentstack.dart' as contentstack;
Future<void> main() async {
final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');
const pageSize = 100;
var offset = 0;
while (true) {
final query = stack.assetQuery()
..limit(pageSize)
..skip(offset);
final response = await query.find();
final assets = response['assets'] as List;
for (final asset in assets) {
print(asset['title']);
}
// A page shorter than pageSize is the last one
if (assets.length < pageSize) break;
offset += pageSize;
}
}includeBranch
includeBranch asks the API to add the _branch key, the UID of the branch that holds the asset, to the response.
This method takes no parameters.
Returns: void. Nothing. The query keeps the setting for its next request.
Validation
- There is no client-side validation. includeBranch takes no input and cannot fail.
- Errors that do occur come from the find() call that follows, not from includeBranch(). The API rejection comes back as the result, not as an exception. Check the result for an error_code key.
Additional Resource Refer to Content Delivery API Errors for the full error code list.
Behavior
- Client-side only. includeBranch sets the include_branch query parameter to 'true' and makes no request. The find() call that follows sends it.
- The API adds the _branch key, which states the unique ID of the branch where the asset resides. See include_branch in Get all assets.
- includeBranch does not choose the branch. The branch you pass to the Stack constructor goes out as the branch header and chooses it.
- Calling includeBranch twice changes nothing, because both calls write the same value under the same key.
Example
Basic usage: list the assets of a branch with their branch UID
import 'package:contentstack/contentstack.dart' as contentstack;
Future<void> main() async {
final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>', branch: '<BRANCH>');
final query = stack.assetQuery()..includeBranch();
try {
final response = await query.find();
if (response is Map && response.containsKey('error_code')) {
// The API rejected the request
print('Request failed: ${response['error_code']} ${response['error_message']}');
} else {
for (final asset in response['assets']) {
print('${asset['title']} ${asset['_branch']}');
}
}
} catch (e) {
// Thrown for a non-JSON response, a network failure or a 30-second timeout
print('Request did not complete: $e');
}
}includeCount
includeCount asks the API to add the total number of matching assets to the response.
This method takes no parameters.
Returns: void. Nothing. The query keeps the setting for its next request.
Validation
- There is no client-side validation. includeCount takes no input and cannot fail.
- Errors that do occur come from the find() call that follows, not from includeCount(). The API rejection comes back as the result, not as an exception. Check the result for an error_code key.
Additional Resource Refer to Content Delivery API Errors for the full error code list.
Behavior
- Client-side only. includeCount sets the include_count query parameter to 'true' and makes no request. The find() call that follows sends it.
- The API adds a count key next to assets, and still returns the assets. See Count, which works for assets as well as entries.
- Compare count with limit and skip to work out the number of pages to request.
- Calling includeCount twice changes nothing, because both calls write the same value under the same key.
Example
Basic usage: print the total number of assets
import 'package:contentstack/contentstack.dart' as contentstack;
Future<void> main() async {
final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');
final query = stack.assetQuery()..includeCount();
try {
final response = await query.find();
if (response is Map && response.containsKey('error_code')) {
// The API rejected the request
print('Request failed: ${response['error_code']} ${response['error_message']}');
} else {
print('Total: ${response['count']}, on this page: ${response['assets'].length}');
}
} catch (e) {
// Thrown for a non-JSON response, a network failure or a 30-second timeout
print('Request did not complete: $e');
}
}includeDimension
includeDimension asks the API to add the height and width of each image asset to the response.
This method takes no parameters.
Returns: void. Nothing. The query keeps the setting for its next request.
Validation
- There is no client-side validation. includeDimension takes no input and cannot fail.
- Errors that do occur come from the find() call that follows, not from includeDimension(). The API rejection comes back as the result, not as an exception. Check the result for an error_code key.
Additional Resource Refer to Content Delivery API Errors for the full error code list.
Behavior
- Client-side only. includeDimension sets the include_dimension query parameter to 'true' and makes no request. The find() call that follows sends it.
- The API adds a dimension key, with height and width, to each JPG, GIF, PNG, WebP, BMP, TIFF, SVG and PSD image. See include_dimension in Get all assets.
- Calling includeDimension twice changes nothing, because both calls write the same value under the same key.
Example
Basic usage: list images with their height and width
import 'package:contentstack/contentstack.dart' as contentstack;
Future<void> main() async {
final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');
final query = stack.assetQuery()..includeDimension();
try {
final response = await query.find();
if (response is Map && response.containsKey('error_code')) {
// The API rejected the request
print('Request failed: ${response['error_code']} ${response['error_message']}');
} else {
for (final asset in response['assets']) {
// Assets that are not images carry no dimension key
print('${asset['title']} ${asset['dimension']}');
}
}
} catch (e) {
// Thrown for a non-JSON response, a network failure or a 30-second timeout
print('Request did not complete: $e');
}
}includeFallback
includeFallback asks the API to return the asset details published in the fallback locale when the requested locale has none.
This method takes no parameters.
Returns: void. Nothing. The query keeps the setting for its next request.
Validation
- There is no client-side validation. includeFallback takes no input and cannot fail.
- Errors that do occur come from the find() call that follows, not from includeFallback(). The API rejection comes back as the result, not as an exception. Check the result for an error_code key.
Additional Resource Refer to Content Delivery API Errors for the full error code list.
Behavior
- Client-side only. includeFallback sets the include_fallback query parameter to 'true' and makes no request. The find() call that follows sends it.
- The API includes the published asset details from the fallback locale when the specified locale has no published content. See include_fallback in Get all assets.
- AssetQuery has no method that sets a locale, and the Get all assets reference lists no locale query parameter.
- Calling includeFallback twice changes nothing, because both calls write the same value under the same key.
Example
Basic usage: list assets with fallback-locale details
import 'package:contentstack/contentstack.dart' as contentstack;
Future<void> main() async {
final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');
final query = stack.assetQuery()..includeFallback();
try {
final response = await query.find();
if (response is Map && response.containsKey('error_code')) {
// The API rejected the request
print('Request failed: ${response['error_code']} ${response['error_message']}');
} else {
for (final asset in response['assets']) {
print('${asset['title']} ${asset['publish_details']}');
}
}
} catch (e) {
// Thrown for a non-JSON response, a network failure or a 30-second timeout
print('Request did not complete: $e');
}
}limit
limit caps the number of assets on the page that find returns.
| Name | Type | Description |
|---|---|---|
| limitCount (required) | int | Maximum number of assets on the page. |
Returns: void. Nothing. The query keeps the setting for its next request.
Validation
- There is no client-side validation. limit stores any integer as a string, including 0 and negative numbers.
- The API accepts positive integers only. A limit of 0 returns the default number of records rather than every asset. See Limit.
- An invalid value surfaces when find() sends the request. The API rejection comes back as the result, not as an exception. Check the result for an error_code key.
Additional Resource Refer to Content Delivery API Errors for the full error code list.
Behavior
- Client-side only. limit sets the limit query parameter and makes no request. The find() call that follows sends it.
- The Limit parameter works for assets as well as entries. Without it, the API returns the first 100 assets. See Pagination.
- Pair limit with skip to request later pages. For example, limit(10) with skip(10) returns assets 11 to 20.
- A second call replaces the first value, because both calls write the same key.
Example
Basic usage: list the first five assets
import 'package:contentstack/contentstack.dart' as contentstack;
Future<void> main() async {
final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');
final query = stack.assetQuery()..limit(5);
try {
final response = await query.find();
if (response is Map && response.containsKey('error_code')) {
// The API rejected the request
print('Request failed: ${response['error_code']} ${response['error_message']}');
} else {
for (final asset in response['assets']) {
print(asset['title']);
}
}
} catch (e) {
// Thrown for a non-JSON response, a network failure or a 30-second timeout
print('Request did not complete: $e');
}
}orderByAscending
orderByAscending sorts the assets that find returns in ascending order of one field.
| Name | Type | Description |
|---|---|---|
| key (required) | String | UID of the asset field to sort by. |
Returns: void. Nothing. The query keeps the setting for its next request.
Validation
- There is no client-side validation. orderByAscending stores any string, including an empty one.
- An invalid value surfaces when find() sends the request. The API rejection comes back as the result, not as an exception. Check the result for an error_code key.
Additional Resource Refer to Content Delivery API Errors for the full error code list.
Behavior
- Client-side only. orderByAscending sets the asc query parameter to the field UID and makes no request. The find() call that follows sends it.
- The asc parameter works for assets as well as entries. See Order by asc.
- A second call replaces the first field, because both calls write the same key. The orderByDescending method writes a different key, so a query that calls both sends both.
Example
Basic usage: list the oldest assets first
import 'package:contentstack/contentstack.dart' as contentstack;
Future<void> main() async {
final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');
final query = stack.assetQuery()..orderByAscending('created_at');
try {
final response = await query.find();
if (response is Map && response.containsKey('error_code')) {
// The API rejected the request
print('Request failed: ${response['error_code']} ${response['error_message']}');
} else {
for (final asset in response['assets']) {
print('${asset['created_at']} ${asset['title']}');
}
}
} catch (e) {
// Thrown for a non-JSON response, a network failure or a 30-second timeout
print('Request did not complete: $e');
}
}orderByDescending
orderByDescending sorts the assets that find returns in descending order of one field.
| Name | Type | Description |
|---|---|---|
| key (required) | String | UID of the asset field to sort by. |
Returns: void. Nothing. The query keeps the setting for its next request.
Validation
- There is no client-side validation. orderByDescending stores any string, including an empty one.
- An invalid value surfaces when find() sends the request. The API rejection comes back as the result, not as an exception. Check the result for an error_code key.
Additional Resource Refer to Content Delivery API Errors for the full error code list.
Behavior
- Client-side only. orderByDescending sets the desc query parameter to the field UID and makes no request. The find() call that follows sends it.
- The desc parameter works for assets as well as entries. See Order by desc.
- A second call replaces the first field, because both calls write the same key. The orderByAscending method writes a different key, so a query that calls both sends both.
Example
Basic usage: list the newest assets first
import 'package:contentstack/contentstack.dart' as contentstack;
Future<void> main() async {
final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');
final query = stack.assetQuery()..orderByDescending('created_at');
try {
final response = await query.find();
if (response is Map && response.containsKey('error_code')) {
// The API rejected the request
print('Request failed: ${response['error_code']} ${response['error_message']}');
} else {
for (final asset in response['assets']) {
print('${asset['created_at']} ${asset['title']}');
}
}
} catch (e) {
// Thrown for a non-JSON response, a network failure or a 30-second timeout
print('Request did not complete: $e');
}
}param
param adds one query parameter, by name and value, to the request that find sends.
| Name | Type | Description |
|---|---|---|
| key (required) | String | Name of the query parameter to send. |
| value (required) | String | Value of the query parameter. |
Returns: void. Nothing. The query keeps the setting for its next request.
Validation
- param stores nothing and raises no error when key or value is an empty string.
- param does not check the name. An unknown parameter surfaces when find() sends the request. The API rejection comes back as the result, not as an exception. Check the result for an error_code key.
Additional Resource Refer to Content Delivery API Errors for the full error code list.
Behavior
- Client-side only. param writes the value under key in the query's top-level parameters and makes no request. The find() call that follows sends it.
- param replaces any value already stored under the same key, including the values that other methods write, such as environment or limit.
- Use param for a parameter that AssetQuery has no method for. The Get all assets reference lists include_metadata and asset_fields[], for example.
- param also sends a field condition. A JSON string under the query key goes out as the query parameter. The Equals Operator and the other query operators work for assets as well as entries.
Example
Basic usage: include the metadata of each asset
import 'package:contentstack/contentstack.dart' as contentstack;
Future<void> main() async {
final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');
final query = stack.assetQuery()..param('include_metadata', 'true');
try {
final response = await query.find();
if (response is Map && response.containsKey('error_code')) {
// The API rejected the request
print('Request failed: ${response['error_code']} ${response['error_message']}');
} else {
for (final asset in response['assets']) {
print('${asset['title']} ${asset['_metadata']}');
}
}
} catch (e) {
// Thrown for a non-JSON response, a network failure or a 30-second timeout
print('Request did not complete: $e');
}
}Filter by field value: send a query condition as JSON
import 'dart:convert';
import 'package:contentstack/contentstack.dart' as contentstack;
Future<void> main() async {
final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');
final query = stack.assetQuery()
..param('query', jsonEncode({'title': 'Hero banner'}));
final response = await query.find();
for (final asset in response['assets']) {
print('${asset['uid']} ${asset['title']}');
}
}query
query stores a condition that one asset field equals a string, but find does not send it on an asset query.
| Name | Type | Description |
|---|---|---|
| key (required) | String | UID of the asset field to match. |
| value (required) | String | Value the field must equal. |
Returns: void. Nothing. The query keeps the setting for its next request.
Validation
- query stores nothing and raises no error when key or value is an empty string.
- Because find does not send the condition, query cannot cause an API error on an asset query. Errors that do occur come from the find() call that follows. The API rejection comes back as the result, not as an exception. Check the result for an error_code key.
Known issue find does not send the condition that query stores, so the request returns every asset, unfiltered. Send the condition as JSON with the param method, param('query', jsonEncode({key: value})), or filter the returned list in your app.
Additional Resource Refer to Content Delivery API Errors for the full error code list.
Behavior
- Client-side only. query writes the condition into the query's field conditions and makes no request.
- query writes to the same conditions as the where and addQuery methods, so it replaces a condition already stored on the same field.
- On a Query for entries, find sends the stored conditions as the query parameter.
Example
Workaround: send the condition with param instead of query
import 'dart:convert';
import 'package:contentstack/contentstack.dart' as contentstack;
Future<void> main() async {
final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');
// query stores the condition, but find does not send it on an asset query
final ignored = stack.assetQuery()..query('filename', 'hero-banner.png');
// param sends the same condition as the query parameter
final filtered = stack.assetQuery()
..param('query', jsonEncode({'filename': 'hero-banner.png'}));
try {
final all = await ignored.find();
final matching = await filtered.find();
if (matching is Map && matching.containsKey('error_code')) {
// The API rejected the request
print('Request failed: ${matching['error_code']} ${matching['error_message']}');
} else {
print('Unfiltered: ${all['assets'].length}, matching: ${matching['assets'].length}');
}
} catch (e) {
// Thrown for a non-JSON response, a network failure or a 30-second timeout
print('Request did not complete: $e');
}
}relativeUrls
relativeUrls sets the relative_urls query parameter to 'true' on the asset query.
This method takes no parameters.
Returns: void. Nothing. The query keeps the setting for its next request.
Validation
- There is no client-side validation. relativeUrls takes no input and cannot fail.
- Errors that do occur come from the find() call that follows, not from relativeUrls(). The API rejection comes back as the result, not as an exception. Check the result for an error_code key.
Additional Resource Refer to Content Delivery API Errors for the full error code list.
Behavior
- Client-side only. relativeUrls sets the relative_urls query parameter to 'true' and makes no request. The find() call that follows sends it.
- The Get all assets reference does not list relative_urls. Print the url of a returned asset, as the example below does, to see the form your stack returns.
- Calling relativeUrls twice changes nothing, because both calls write the same value under the same key.
Example
Basic usage: list assets with the relative_urls parameter
import 'package:contentstack/contentstack.dart' as contentstack;
Future<void> main() async {
final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');
final query = stack.assetQuery()..relativeUrls();
try {
final response = await query.find();
if (response is Map && response.containsKey('error_code')) {
// The API rejected the request
print('Request failed: ${response['error_code']} ${response['error_message']}');
} else {
for (final asset in response['assets']) {
print('${asset['title']} ${asset['url']}');
}
}
} catch (e) {
// Thrown for a non-JSON response, a network failure or a 30-second timeout
print('Request did not complete: $e');
}
}skip
skip sets the number of assets the API skips before the page that find returns.
| Name | Type | Description |
|---|---|---|
| skipCount (required) | int | Number of assets to skip before the page. |
Returns: void. Nothing. The query keeps the setting for its next request.
Validation
- There is no client-side validation. skip stores any integer as a string, including negative numbers.
- An invalid value surfaces when find() sends the request. The API rejection comes back as the result, not as an exception. Check the result for an error_code key.
Additional Resource Refer to Content Delivery API Errors for the full error code list.
Behavior
- Client-side only. skip sets the skip query parameter and makes no request. The find() call that follows sends it.
- The Skip parameter works for assets as well as entries. See Skip.
- Pair skip with limit to request a page of a fixed size, and with includeCount to know when to stop.
- A second call replaces the first value, because both calls write the same key.
Example
Basic usage: list the second page of 20 assets
import 'package:contentstack/contentstack.dart' as contentstack;
Future<void> main() async {
final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');
final query = stack.assetQuery()
..limit(20)
..skip(20);
try {
final response = await query.find();
if (response is Map && response.containsKey('error_code')) {
// The API rejected the request
print('Request failed: ${response['error_code']} ${response['error_message']}');
} else {
for (final asset in response['assets']) {
print(asset['title']);
}
}
} catch (e) {
// Thrown for a non-JSON response, a network failure or a 30-second timeout
print('Request did not complete: $e');
}
}version
version sets the version query parameter that find sends for this query.
| Name | Type | Description |
|---|---|---|
| version (required) | int | Asset version number to send. |
Returns: void. Nothing. The query keeps the setting for its next request.
Validation
- There is no client-side validation. version stores any integer as a string, including 0 and negative numbers.
- An invalid value surfaces when find() sends the request. The API rejection comes back as the result, not as an exception. Check the result for an error_code key.
Additional Resource Refer to Content Delivery API Errors for the full error code list.
Behavior
- Client-side only. version sets the version query parameter and makes no request. The find() call that follows sends it.
- The Get all assets reference does not list a version parameter. The Get a single asset reference does, so to read one version of one asset, use the version method of Asset.
- A second call replaces the first value, because both calls write the same key.
Example
Basic usage: send a version number with the asset list request
import 'package:contentstack/contentstack.dart' as contentstack;
Future<void> main() async {
final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');
final query = stack.assetQuery()..version(2);
try {
final response = await query.find();
if (response is Map && response.containsKey('error_code')) {
// The API rejected the request
print('Request failed: ${response['error_code']} ${response['error_message']}');
} else {
for (final asset in response['assets']) {
print('${asset['title']} version ${asset['_version']}');
}
}
} catch (e) {
// Thrown for a non-JSON response, a network failure or a 30-second timeout
print('Request did not complete: $e');
}
}where
where stores a condition on one asset field with a comparison operator, but find does not send it on an asset query.
| Name | Type | Description |
|---|---|---|
| fieldUid (required) | String | UID of the asset field to compare, such as title. |
| queryOperation (required) | QueryOperation | Operator and value to compare against. |
Returns: void. Nothing. The query keeps the setting for its next request.
Validation
- where stores nothing and raises no error when fieldUid contains a character other than a letter, a digit, -, _ or ..
- where does not check the value inside queryOperation.
- Because find does not send the condition, where cannot cause an API error on an asset query. Errors that do occur come from the find() call that follows. The API rejection comes back as the result, not as an exception. Check the result for an error_code key.
Known issue find does not send the condition that where stores, so the request returns every asset, unfiltered. Send the condition as JSON with the param method, param('query', jsonEncode({...})), or filter the returned list in your app.
Additional Resource Refer to Content Delivery API Errors for the full error code list.
Behavior
- Client-side only. where writes the condition into the query's field conditions and makes no request.
- The main library does not export QueryOperation or QueryOperationType. Import both from package:contentstack/src/enums/, as the example below does.
- QueryOperation(operationType, value) takes both arguments by position. Each QueryOperationType value maps to one operator, as the table below shows.
| QueryOperationType | Operator stored |
|---|---|
| Equals | The value itself, with no operator |
| NotEquals | $ne |
| Includes | $in |
| Excludes | $nin |
| IsLessThan | $lt |
| IsLessThanOrEqual | $lte |
| IsGreaterThan | $gt |
| IsGreaterThanOrEqual | $gte |
| Exists | $exists |
| Matches | $regex |
- A second call on the same field replaces the first condition. The addQuery and query methods write to the same conditions.
- On a Query for entries, find sends the stored conditions as the query parameter.
Example
Workaround: send the condition with param instead of where
import 'dart:convert';
import 'package:contentstack/contentstack.dart' as contentstack;
import 'package:contentstack/src/enums/operations.dart';
import 'package:contentstack/src/enums/operations_type.dart';
Future<void> main() async {
final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');
// where stores the condition, but find does not send it on an asset query
final ignored = stack.assetQuery()
..where('title', QueryOperation(QueryOperationType.Matches, '^Hero'));
// param sends the same condition as the query parameter
final filtered = stack.assetQuery()
..param('query', jsonEncode({'title': {'\$regex': '^Hero'}}));
try {
final all = await ignored.find();
final matching = await filtered.find();
if (matching is Map && matching.containsKey('error_code')) {
// The API rejected the request
print('Request failed: ${matching['error_code']} ${matching['error_message']}');
} else {
print('Unfiltered: ${all['assets'].length}, matching: ${matching['assets'].length}');
}
} catch (e) {
// Thrown for a non-JSON response, a network failure or a 30-second timeout
print('Request did not complete: $e');
}
}