ContentTypeQuery

View as Markdown

ContentTypeQuery

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.

ContentTypeQuery lists the content types of a stack in one request. Use this class to

  • list every content type with its schema
  • add the total count of content types to the response
  • add the schemas of global fields to each content type schema
  • send extra query parameters, such as include_branch

stack.contentType().query() returns a new query and takes no arguments. The class inherits its pagination, sorting and parameter methods from BaseQuery, which the main library does not export.

Class-Level Notes

Setters collect values until find.

  • Every method except find stores a value on the query and returns void. Chain them with the cascade operator, for example stack.contentType().query()..includeCount()..limit(10).
  • The query keeps every value you set, and no method removes one. A second call with the same key replaces the value. Create a new query for a request with different settings.

Filters have no effect.

  • Known issue: find does not send the conditions from where, query and addQuery, so the API returns every content type. Filter the content_types list in your app instead.

Parameters of the request.

  • find sends one Get all content types request. That reference lists include_count, include_branch and include_global_field_schema.
  • The SDK also sends limit, skip, asc and desc when you set them. The reference does not list them for this request.

Imports for where.

  • where takes a QueryOperation, which the main library does not export. Import package:contentstack/src/enums/operations.dart and package:contentstack/src/enums/operations_type.dart to call it.

Capability Matrix

CapabilitySupportedNotes / Alternative
Filter content types by a field valueNoFilter the content_types list in your app
Fetch every page of content types in one callNoCall find again with a larger skip value
A typed model for content typesNoCall find without type arguments and read the Map

Method Index

The groups below follow what you want to do. Every method is available on every ContentTypeQuery.

Send the request. Available on every query. This is the only method that makes a request.

Method NameReturnsDescription
findThe matching content types.Sends the request and returns the content types.

Add data to the response. Available on every query.

Method NameReturnsDescription
includeCountNothing. The query keeps the setting for its next request.Adds the total content type count.
includeGlobalFieldNothing. The query keeps the setting for its next request.Adds the schemas of global fields.

Page and sort. Available on every query.

Method NameReturnsDescription
limitNothing. The query keeps the setting for its next request.Caps the number of content types returned.
skipNothing. The query keeps the setting for its next request.Skips a number of content types.
orderByAscendingNothing. The query keeps the setting for its next request.Sorts by a field in ascending order.
orderByDescendingNothing. The query keeps the setting for its next request.Sorts by a field in descending order.

Add query parameters. Available on every query.

Method NameReturnsDescription
paramNothing. The query keeps the setting for its next request.Adds one query parameter.
addParamsNothing. The query keeps the setting for its next request.Adds several query parameters.

Filter by field value. Available on every query. See Filters have no effect above.

Method NameReturnsDescription
whereNothing. The query keeps the setting for its next request.Stores a condition on one field.
queryNothing. The query keeps the setting for its next request.Stores one key and value as a condition.
addQueryNothing. The query keeps the setting for its next request.Stores several conditions.

Class-Level Snippet

This snippet lists the first ten content types with the total count and the schemas of their global fields.

import 'package:contentstack/contentstack.dart' as contentstack;

Future<void> main() async {
  final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');
  final query = stack.contentType().query()
    ..includeCount()
    ..includeGlobalField()
    ..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: ${response['count']}');
      for (final contentType in response['content_types']) {
        print(contentType['uid']);
      }
    }
  } 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 ContentTypeQuery class has its own section below, with its parameters, validation rules, behavior, and examples.

addParams

addParams adds several query parameters to the request that find sends.

NameTypeDescription
parameters (required)Map

Query parameter names and values, all strings.

Default: Not applicable

Returns: void. Nothing. The query keeps the setting for its next request.

Validation

  • An empty map changes nothing and raises no error.
  • Throws TypeError if a key is not a String or a value is not a String, because the query stores every parameter as a string. For example, {'include_branch': true} throws.
  • 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. addParams stores each pair as a query parameter and makes no request. find sends them.
  • A key the query already holds gets the new value.
  • Use addParams for parameters that have no method of their own, such as include_branch from the Get all content types reference.

Example

Basic usage: add two parameters at once

import 'package:contentstack/contentstack.dart' as contentstack;

Future<void> main() async {
  final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');
  final query = stack.contentType().query()
    ..addParams({'include_branch': 'true', 'include_count': '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 {
      print('content types: ${response['count']}');
    }
  } 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 keys and values as conditions on the content types.

NameTypeDescription
parameters (required)Map

Field UIDs and the values or operator maps to match.

Default: Not applicable

Returns: void. Nothing. The query keeps the setting for its next request.

Validation

  • An empty map changes nothing and raises no error.
  • Throws TypeError if a key is not a String, because the query stores conditions under string keys.
  • 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

  • Known issue: find does not send this condition, so the API returns every content type. Filter the content_types list in your app instead, as the second example shows.
  • Client-side only. addQuery stores each pair as a condition and makes no request. A value can be a plain value or an operator map, such as {'$in': ['Blog', 'Author']}.
  • A key the query already holds gets the new value.

Example

Basic usage: store several conditions

import 'package:contentstack/contentstack.dart' as contentstack;

Future<void> main() async {
  final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');
  final query = stack.contentType().query()
    ..addQuery({'title': {r'$in': ['Blog', 'Author']}});

  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 contentType in response['content_types']) {
        print(contentType['title']);
      }
    }
  } catch (e) {
    // Thrown for a non-JSON response, a network failure or a 30-second timeout
    print('Request did not complete: $e');
  }
}

Workaround: filter the content types in your app

import 'package:contentstack/contentstack.dart' as contentstack;

Future<void> main() async {
  final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');
  const titles = ['Blog', 'Author'];

  final response = await stack.contentType().query().find();
  for (final contentType in response['content_types']) {
    if (titles.contains(contentType['title'])) {
      print(contentType['uid']);
    }
  }
}

find

find sends the query and returns the content types of the stack.

NameTypeDescription
queryParamsMap<String, String>? (named)

Extra query parameters for the request.

Default: null

Returns: Future<T>. The matching content types.

Validation

  • There is no client-side validation of queryParams.
  • Errors from the request:
    • find throws only when the request does not complete: a network failure, a 30-second timeout or a response that is not JSON.
    • An API rejection comes back as the result with an error_code key. It does not throw.
    • Check the result for an error_code key even inside a try block.

Additional Resource Refer to Content Delivery API Errors for the full error code list.

Behavior

  • One call sends one Get all content types request, GET /<apiVersion>/content_types, with the environment query parameter and every parameter the query holds.
  • find adds queryParams to the parameters of the query before it sends them. A later find on the same query sends them again.
  • Known issue: find does not send the conditions from where, query and addQuery, so the API returns every content type. Filter the content_types list in your app instead.
  • The result is the decoded JSON body as a Map. The content types are in its content_types list, and the total is in count when you call includeCount.
  • The SDK has no model class for content types. Call find without type arguments.
  • find returns one page. It does not request the pages after it. Use skip and limit to read more.

Example

Basic usage: list the content types

import 'package:contentstack/contentstack.dart' as contentstack;

Future<void> main() async {
  final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');
  final query = stack.contentType().query();

  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 contentType in response['content_types']) {
        print(contentType['title']);
      }
    }
  } catch (e) {
    // Thrown for a non-JSON response, a network failure or a 30-second timeout
    print('Request did not complete: $e');
  }
}

All parameters: add the branch key to every content type

import 'package:contentstack/contentstack.dart' as contentstack;

Future<void> main() async {
  final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');
  final query = stack.contentType().query();

  final response = await query.find(queryParams: {'include_branch': 'true'});
  for (final contentType in response['content_types']) {
    print('${contentType['uid']}: ${contentType['_branch']}');
  }
}

includeCount

includeCount asks the API to add the total number of content types 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 cannot fail.
  • 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. includeCount stores include_count=true on the query and makes no request. find sends it.
  • The Get all content types request returns the total in the count key, beside content_types.
  • A second call changes nothing, and no method removes the setting.

Example

Basic usage: read the total number of content types

import 'package:contentstack/contentstack.dart' as contentstack;

Future<void> main() async {
  final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');
  final query = stack.contentType().query()..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('content types: ${response['count']}');
    }
  } catch (e) {
    // Thrown for a non-JSON response, a network failure or a 30-second timeout
    print('Request did not complete: $e');
  }
}

includeGlobalField

includeGlobalField asks the API to add the schema of each global field to the content type schemas.

This method takes no parameters.

Returns: void. Nothing. The query keeps the setting for its next request.

Validation

  • There is no client-side validation. includeGlobalField cannot fail.
  • 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. includeGlobalField stores include_global_field_schema=true on the query and makes no request. find sends it.
  • Without it, a content type that contains a global field does not carry the schema of that global field. See the tip in Get all content types.
  • A second call changes nothing, and no method removes the setting.

Example

Basic usage: list content types with their global field schemas

import 'package:contentstack/contentstack.dart' as contentstack;

Future<void> main() async {
  final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');
  final query = stack.contentType().query()..includeGlobalField();

  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 contentType in response['content_types']) {
        print('${contentType['uid']}: ${contentType['schema'].length} fields');
      }
    }
  } 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 content types that find returns.

NameTypeDescription
limitCount (required)int

Maximum number of content types to return.

Default: Not applicable

Returns: void. Nothing. The query keeps the setting for its next request.

Validation

  • There is no client-side validation. limit accepts zero 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. limit stores the number as the limit query parameter and makes no request. find sends it.
  • The Get all content types reference does not list limit among its parameters. Compare the number of content types returned with count from includeCount to see whether more remain.
  • A second call replaces the value.
  • Use the skip method with limit to read the list one page at a time.

Example

Basic usage: list the first five content types

import 'package:contentstack/contentstack.dart' as contentstack;

Future<void> main() async {
  final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');
  final query = stack.contentType().query()..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 contentType in response['content_types']) {
        print(contentType['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 content types that find returns by one field, from lowest to highest.

NameTypeDescription
key (required)String

UID of the field to sort by, such as title.

Default: Not applicable

Returns: void. Nothing. The query keeps the setting for its next request.

Validation

  • There is no client-side validation. orderByAscending accepts 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 stores the field as the asc query parameter and makes no request. find sends it.
  • The Get all content types reference does not list asc among its parameters.
  • A second call replaces the field. A call to orderByDescending stores a separate desc parameter, so the query then sends both.

Example

Basic usage: sort content types by title

import 'package:contentstack/contentstack.dart' as contentstack;

Future<void> main() async {
  final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');
  final query = stack.contentType().query()..orderByAscending('title');

  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 contentType in response['content_types']) {
        print(contentType['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 content types that find returns by one field, from highest to lowest.

NameTypeDescription
key (required)String

UID of the field to sort by, such as updated_at.

Default: Not applicable

Returns: void. Nothing. The query keeps the setting for its next request.

Validation

  • There is no client-side validation. orderByDescending accepts 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 stores the field as the desc query parameter and makes no request. find sends it.
  • The Get all content types reference does not list desc among its parameters.
  • A second call replaces the field. A call to orderByAscending stores a separate asc parameter, so the query then sends both.

Example

Basic usage: list the most recently updated content types first

import 'package:contentstack/contentstack.dart' as contentstack;

Future<void> main() async {
  final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');
  final query = stack.contentType().query()..orderByDescending('updated_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 contentType in response['content_types']) {
        print('${contentType['title']}: ${contentType['updated_at']}');
      }
    }
  } 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 to the request that find sends.

NameTypeDescription
key (required)String

Name of the query parameter.

Default: Not applicable
value (required)String

Value of the query parameter.

Default: Not applicable

Returns: void. Nothing. The query keeps the setting for its next request.

Validation

  • param makes no change and raises no error when key or value is an empty string.
  • 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. param stores the pair as a query parameter and makes no request. find sends it.
  • A key the query already holds gets the new value, including keys that other methods set, such as limit.
  • Use param for a parameter that has no method of its own, such as include_branch from the Get all content types reference.
  • Use the addParams method to add several parameters in one call.

Example

Basic usage: add the branch key to every content type

import 'package:contentstack/contentstack.dart' as contentstack;

Future<void> main() async {
  final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');
  final query = stack.contentType().query()..param('include_branch', '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 contentType in response['content_types']) {
        print('${contentType['uid']}: ${contentType['_branch']}');
      }
    }
  } catch (e) {
    // Thrown for a non-JSON response, a network failure or a 30-second timeout
    print('Request did not complete: $e');
  }
}

query

query stores one key and value as a condition on the content types.

NameTypeDescription
key (required)String

UID of the field to compare.

Default: Not applicable
value (required)String

Value the field must equal.

Default: Not applicable

Returns: void. Nothing. The query keeps the setting for its next request.

Validation

  • query makes no change and raises no error when key or value is an empty string.
  • 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

  • Known issue: find does not send this condition, so the API returns every content type. Filter the content_types list in your app instead, as the second example shows.
  • Client-side only. query stores the pair as an equality condition and makes no request.
  • A second call with the same key replaces the value.

Example

Basic usage: store a condition as a key and a value

import 'package:contentstack/contentstack.dart' as contentstack;

Future<void> main() async {
  final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');
  final query = stack.contentType().query()..query('title', 'Blog');

  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 contentType in response['content_types']) {
        print(contentType['title']);
      }
    }
  } catch (e) {
    // Thrown for a non-JSON response, a network failure or a 30-second timeout
    print('Request did not complete: $e');
  }
}

Workaround: filter the content types in your app

import 'package:contentstack/contentstack.dart' as contentstack;

Future<void> main() async {
  final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');

  final response = await stack.contentType().query().find();
  for (final contentType in response['content_types']) {
    if (contentType['title'] == 'Blog') {
      print(contentType['uid']);
    }
  }
}

skip

skip makes find leave out a number of content types from the start of the list.

NameTypeDescription
skipCount (required)int

Number of content types to leave out.

Default: Not applicable

Returns: void. Nothing. The query keeps the setting for its next request.

Validation

  • There is no client-side validation. skip accepts zero 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. skip stores the number as the skip query parameter and makes no request. find sends it.
  • The Get all content types reference does not list skip among its parameters.
  • A second call replaces the value.
  • Combine skip with the limit method to read the list one page at a time.

Example

Basic usage: read the second page of content types

import 'package:contentstack/contentstack.dart' as contentstack;

Future<void> main() async {
  final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');
  final query = stack.contentType().query()
    ..skip(10)
    ..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 {
      for (final contentType in response['content_types']) {
        print(contentType['title']);
      }
    }
  } 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 field of the content types.

NameTypeDescription
fieldUid (required)String

UID of the field to compare, such as title.

Default: Not applicable
queryOperation (required)QueryOperation

Operation type and value to compare with.

Default: Not applicable

Returns: void. Nothing. The query keeps the setting for its next request.

Validation

  • where makes no change and raises no error when fieldUid is empty or holds a character outside letters, digits, -, _ and .. The SDK accepts only those characters in a field UID.
  • 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

  • Known issue: find does not send this condition, so the API returns every content type. Filter the content_types list in your app instead, as the second example shows.
  • Client-side only. where stores the condition under fieldUid, in the form the Queries reference describes for entries, and makes no request.
  • QueryOperationType sets the operator, as the table below shows.
QueryOperationTypeStored condition
Equalsthe value itself
NotEquals$ne
Includes$in
Excludes$nin
IsLessThan$lt
IsLessThanOrEqual$lte
IsGreaterThan$gt
IsGreaterThanOrEqual$gte
Exists$exists
Matches$regex
  • A second condition on the same field replaces the first.
  • The main library does not export QueryOperation or QueryOperationType. Import them from package:contentstack/src/enums/operations.dart and package:contentstack/src/enums/operations_type.dart.

Example

Basic usage: store a condition on the title

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>');
  final query = stack.contentType().query()
    ..where('title', QueryOperation(QueryOperationType.Equals, 'Blog'));

  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 contentType in response['content_types']) {
        print(contentType['title']);
      }
    }
  } catch (e) {
    // Thrown for a non-JSON response, a network failure or a 30-second timeout
    print('Request did not complete: $e');
  }
}

Workaround: filter the content types in your app

import 'package:contentstack/contentstack.dart' as contentstack;

Future<void> main() async {
  final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');

  final response = await stack.contentType().query().find();
  final blogs = response['content_types'].where((contentType) => contentType['title'] == 'Blog');
  print(blogs.map((contentType) => contentType['uid']).toList());
}