Overview
ThedynamicView widget allows you to fetch data from an API and render it using a template. This powerful feature enables dynamic content rendering based on remote data sources, making it perfect for creating data-driven UIs without writing custom code.
Features
- Fetch data from any REST API endpoint
- Apply data to templates with placeholder syntax
- Extract nested data using dot notation and array indexing
- Handle both single objects and lists of data
- Render lists of items using the itemTemplate feature
- Customize loading and error states
- Target specific data paths within complex API responses
Properties
| Property | Type | Required | Description |
|---|---|---|---|
| request | StacNetworkRequest | Yes | API request configuration (url, method, headers, etc.) |
| template | Map<String, dynamic> | Yes | Template to render with data from the API response |
| targetPath | String | No | Path to extract specific data from the API response |
| resultTarget | String | No | Key name to use when applying data to the template |
| loaderWidget | Map<String, dynamic> | No | Custom widget to display while loading data |
| errorWidget | Map<String, dynamic> | No | Custom widget to display when an error occurs |
| itemTemplate | Map<String, dynamic> | No | Template to render each item in a list of items from the API response |
| emptyTemplate | Map<String, dynamic> | No | Template to render when the API response contains an empty list |
Basic Usage
Data Placeholders
Use double curly braces{{placeholder}} to insert data from the API response into your template:
- For nested data, use dot notation:
{{user.address.city}} - For array elements, use index notation:
{{users[0].name}}or combined path:{{items.0.title}} - For array elements within objects, use combined notation:
{{data.users[2].profile.name}}
Examples
User Profile Example with Loading and Error States
List Example with itemTemplate
When the API returns a list of items, use theitemTemplate property to define how each item should be rendered:
Empty State Handling
Use theemptyTemplate property to show a user-friendly message when the API returns an empty list:
Empty Template Behavior
TheemptyTemplate is automatically triggered when:
- Direct empty array: The API response is an empty array
[] - Empty array at target path: The data at the specified
targetPathis an empty array - Empty arrays in nested data: The API response contains empty arrays within nested objects
Example API Responses that Trigger Empty Template
Scenario 1: Direct empty arrayAdvanced Usage
Extracting Nested Data
Use thetargetPath property to extract specific data from complex API responses:
Using resultTarget
TheresultTarget property allows you to specify a key name to use when applying data to the template. This is useful when you want to reference the data with a specific name in your template:
Custom Headers
Add custom headers to your API requests:Array Indexing in targetPath
You can access specific array elements in the targetPath:Best Practices
- Use
targetPathto extract only the data you need from complex API responses - For list data, always use the
itemTemplateproperty to define how each item should be rendered - Always provide an
emptyTemplatefor list-based views to handle empty API responses gracefully - Design empty states that are informative and actionable - include clear messaging and relevant actions like refresh buttons
- Keep templates modular and reusable when possible
- Use appropriate error handling in your UI design for cases when the API request fails
- For empty states, use appropriate icons and colors that match your app’s design system
- Provide custom
loaderWidgetanderrorWidgetfor better user experience - Use
resultTargetwhen you need to reference the data with a specific name in your template - Keep templates modular and reusable when possible
Limitations
- API endpoints must return JSON data
- For very large datasets, consider pagination or limiting the number of items to avoid performance issues
- Complex data transformations may require custom code outside of the template system
- Nested array access in placeholder syntax is limited to the formats shown in the examples