includeReference method
- String referenceFieldUid,
- {Include includeReferenceField}
- Include Reference When you fetch an entry of a content type that has a reference field, by default, the content of the referred entry is not fetched. It only fetches the UID of the referred entry, along with the content of the specified entry.
If you wish to fetch the content of the entry that is included in
the reference field, you need to use the include[] parameter,
and specify the UID of the reference field as value.
This informs Contentstack that the request also includes
fetching the entry used in the specified reference field.
Add a constraint that requires a particular reference key details.
includeReference provides three options, none, only and except
i.e accepts list of fieldUid
referenceFieldUid
Key who has reference to some other class object.
Array of the only reference keys to be included in response.
{Example 1}: Reference type None
final stack = contentstack.Stack('apiKey, 'deliveryKey, 'environment);
final query = stack.contentType('contentTypeUid').entry().query();
query.includeReference("referenceFieldUid",
IncludeReference.none(fieldUidList: null));
await entry.fetch();
{Example 2}: Reference type only
final stack = contentstack.Stack('apiKey, 'deliveryKey, 'environment);
final query = stack.contentType('contentTypeUid').entry().query();
final fieldUid = list of string type;
query.includeReference("referenceFieldUid",
IncludeReference.only(fieldUidList: fieldUid));
{Example 3}: Reference type except
final stack = contentstack.Stack('apiKey, 'deliveryKey, 'environment);
final query = stack.contentType('contentTypeUid').entry().query();
query.includeReference("referenceFieldUid",
IncludeReference.except(fieldUidList: fieldUid));
Implementation
void includeReference(String referenceFieldUid,
{include.Include includeReferenceField}) {
if (referenceFieldUid != null && referenceFieldUid.isNotEmpty) {
final List referenceArray = [];
if (includeReferenceField != null) {
includeReferenceField.when(none: (fieldUid) {
referenceArray.add(referenceFieldUid);
if (fieldUid.fieldUidList != null &&
fieldUid.fieldUidList.isNotEmpty) {
for (final item in fieldUid.fieldUidList) {
referenceArray.add(item);
}
}
queryParameter['include[]'] = referenceArray.toString();
}, only: (fieldUid) {
final Map<String, dynamic> referenceOnlyParam = <String, dynamic>{};
if (fieldUid.fieldUidList != null &&
fieldUid.fieldUidList.isNotEmpty) {
for (final item in fieldUid.fieldUidList) {
referenceArray.add(item);
}
}
referenceOnlyParam[referenceFieldUid] = referenceArray;
//_include(referenceFieldUid);
includeReference(referenceFieldUid);
queryParameter['only'] = referenceOnlyParam.toString();
}, except: (fieldUid) {
final Map<String, dynamic> referenceOnlyParam = <String, dynamic>{};
if (fieldUid.fieldUidList != null &&
fieldUid.fieldUidList.isNotEmpty) {
for (final item in fieldUid.fieldUidList) {
referenceArray.add(item);
}
}
referenceOnlyParam[referenceFieldUid] = referenceArray;
//_include(referenceFieldUid);
includeReference(referenceFieldUid);
queryParameter['except'] = referenceOnlyParam.toString();
});
} else {
queryParameter['include[]'] = referenceFieldUid;
}
}
}