includeReference method
- dynamic 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 entry = stack.contentType("contentTypeUid").entry("entryUid");
entry.includeReference("referenceFieldUid",
IncludeReference.none(fieldUidList: null));
response = await entry.fetch();
prints(response)
Example 2: Reference type only
final stack = contentstack.Stack("apiKey", "deliveryKey", "environment");
final entry = stack.contentType("contentTypeUid").entry("entryUid");
final fieldUid = list of string type;
entry.includeReference("referenceFieldUid",
IncludeReference.only(fieldUidList: fieldUid));
response = await entry.fetch();
prints(response)
Example 3: Reference type except
final stack = contentstack.Stack("apiKey", "deliveryKey", "environment");
final entry = stack.contentType("contentTypeUid").entry("entryUid");
entry.includeReference("referenceFieldUid",
IncludeReference.except(fieldUidList: fieldUid));
response = await entry.fetch();
prints(response)
Implementation
void includeReference(referenceFieldUid, {Include includeReferenceField}) {
if (referenceFieldUid != null && referenceFieldUid.isNotEmpty) {
final List referenceArray = [];
if (includeReferenceField != null) {
includeReferenceField.when(none: (fieldUid) {
if (referenceFieldUid.runtimeType == List) {
for (var uid in referenceFieldUid) {
referenceArray.add(uid);
}
} else if (referenceFieldUid.runtimeType == String) {
referenceArray.add(referenceFieldUid);
}
if (fieldUid.fieldUidList != null &&
fieldUid.fieldUidList.isNotEmpty) {
for (final item in fieldUid.fieldUidList) {
referenceArray.add(item);
}
}
parameter['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;
includeReference(referenceFieldUid);
parameter['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;
includeReference(referenceFieldUid);
parameter['except'] = referenceOnlyParam.toString();
});
} else {
parameter['include[]'] = referenceFieldUid;
}
}
}