Was this article helpful?
Thanks for your feedback
This guide will help you get started with Contentstack Dart Utils SDK to build apps powered by Contentstack.
To set up Dart Utils SDK, add the following to your pom.xml file:
contentstack-util: any
Note: If you are using Contentstack Dart SDK, then 'contentstack-utils' is already imported into your project. Thus, in this case, you can see the following line in your pom.xml file:
contentstack: any
Let’s learn how you can use Utils SDK to render embedded items.
To render embedded items on the front-end, use the renderContents function, and define the UI elements you want to show in the front-end of your website, as shown in the example code below:
import 'package:contentstack_utils/src/helper/Metadata.dart'; import 'package:contentstack_utils/src/model/Option.dart'; class OptionDemo implements Option { @override String renderMark(String markType, String renderText) { // TODO: implement renderMark switch (markType) { case 'bold': return '<b>' + renderText + '</b>'; break; default: return ''; } } @override String renderNode(nodeType, Map node_obj, callback) { // TODO: implement renderNode switch (nodeType) { case 'paragraph': String children = callback.renderChildren(node_obj['children']); return "<p class='class-id'>" + children + '</p>'; break; default: return ''; } } @override String renderOption(Map obj, Metadata metadata) { switch (metadata.getStyleType) { case 'block': return '<p>' + obj['title'] + '</p><span>' + obj['multi'] + '</span>'; break; case 'inline': return '<p>' + obj['title'] + '</p><span>' + obj['line'] + '</span>'; break; case 'link': return '<p>' + obj['title'] + '</p><span>' + obj['key'] + '</span>'; break; case 'display': return '<p>' + obj['title'] + '</p><span>' + obj['multi'] + '</span>'; break; default: return ''; } } }
Contentstack Utils SDK lets you interact with the Content Delivery APIs and retrieve embedded items from the RTE field of an entry.
To get an embedded item of a single entry, you need to provide the stack API key, environment name, delivery token, content type’s UID, and entry’s UID. Then, use the includeEmbeddedObjects function as shown below:
import 'package:contentstack/contentstack.dart' as contentstack; final stack = contentstack.Stack(apiKey, deliveryToken, environment); final query = stack.contentType('contentTypeUid').entry().query(); await query.includeEmbeddedObjects().find().then((response) { var entries = response['entries']; const keyPath = [ "rich_text_editor", "global_rich_multiple.group.rich_text_editor" ] entries.forEach((entry){ Utils.render(entry, keyPath, Option); })}).catchError((error) { print(error.message.toString()); }); }
To get embedded items from multiple entries, you need to provide the stack API key, environment name, delivery token, and content type’s UID.
import 'package:contentstack/contentstack.dart' as contentstack; final stack = contentstack.Stack(apiKey, deliveryToken, environment); final query = stack.contentType('contentTypeUid').entry().query(); await query.includeEmbeddedObjects().find().then((response) { var entries = response['entries']; const keyPath = [ "rich_text_editor", "global_rich_multiple.group.rich_text_editor" ] entries.forEach((entry){ Utils.render(entry, keyPath, Option); })}).catchError((error) { print(error.message.toString()); }); }
To get multiple entries, you need to provide the stack API key, environment name, delivery token, content type, and entry UID. Then, use the Utils.jsonToHTML function as shown below:
import 'package:contentstack/contentstack.dart' as contentstack; final stack = contentstack.Stack(apiKey, deliveryToken, environment); final query = stack.contentType('contentTypeUid') .entry() .includeEmbeddedObjects() .query(); await query.find().then((response) { var entries = response['entries']; const keyPath = [ "rich_text_editor" ] entries.forEach((entry){ Utils.jsonToHTML(entry, keyPath, Option); })}).catchError((error) { print(error.message.toString()); }); }
To get multiple entries, you need to provide the stack API key, environment name, delivery token, content type, and entry UID. Then, use the GQL.jsonToHTML function as shown below:
import 'package:contentstack/contentstack.dart' as contentstack; final stack = contentstack.Stack(apiKey, deliveryToken, environment); final query = stack.contentType('contentTypeUid') .entry() .includeEmbeddedObjects() .query(); await query.find().then((response) { var entries = response['entries']; const keyPath = [ "rich_text_editor" ] entries.forEach((entry){ GQL.jsonToHTML(entry, keyPath, Option); })}).catchError((error) { print(error.message.toString()); }); }
Was this article helpful?
Thanks for your feedback