Prerequisites
Before rendering any Stac widgets, you must initialize Stac in your application:Rendering Methods
1. From Stac Cloud (Stac Widget)
The most common approach for server-driven UI is fetching screens from Stac Cloud using the Stac widget.
Usage
routeName and renders it.
Dart Source Code
Thehome_screen is defined in your /stac folder as a Dart file. Here’s an example:
stac/home_screen.dart:
stac deploy, this Dart code is converted to JSON and uploaded to Stac Cloud, making it available via Stac(routeName: 'home_screen').
Properties
| Property | Type | Description |
|---|---|---|
routeName | String | The screen name registered in Stac Cloud |
loadingWidget | Widget? | Custom widget shown while fetching (optional) |
errorWidget | Widget? | Custom widget shown on error (optional) |
Example
When to Use
- ✅ Production apps using Stac Cloud
- ✅ Dynamic content that changes server-side
- ✅ A/B testing and experimentation
- ✅ Apps that need instant updates without app store approval
2. From JSON (Stac.fromJson)
Render a widget directly from a JSON map. Useful for testing, prototyping, or when you have JSON in memory.
Usage
Properties
| Parameter | Type | Description |
|---|---|---|
json | Map<String, dynamic>? | The JSON object representing the widget |
context | BuildContext | The build context |
Example
When to Use
- ✅ Testing and development
- ✅ Prototyping with hardcoded JSON
- ✅ Rendering widgets from local variables
- ✅ Converting existing JSON data to widgets
3. From Assets (Stac.fromAssets)
Load and render widgets from JSON files bundled with your app. Perfect for static content or offline-first scenarios.
Usage
Properties
| Parameter | Type | Description |
|---|---|---|
assetPath | String | Path to the JSON file in your assets folder |
loadingWidget | LoadingWidgetBuilder? | Widget shown while loading (optional) |
errorWidget | ErrorWidgetBuilder? | Widget shown on error (optional) |
Setup
First, add your JSON file topubspec.yaml:
Example
When to Use
- ✅ Static content that doesn’t change
- ✅ Offline-first applications
- ✅ Fallback screens when network fails
- ✅ Demo apps and prototypes
4. From Network (Stac.fromNetwork)
Fetch and render widgets from any HTTP endpoint. Provides more control than Stac Cloud and works with your own API.
Usage
Properties
| Parameter | Type | Description |
|---|---|---|
context | BuildContext | The build context |
request | StacNetworkRequest | Network request configuration |
loadingWidget | LoadingWidgetBuilder? | Widget shown while loading (optional) |
errorWidget | ErrorWidgetBuilder? | Widget shown on error (optional) |
StacNetworkRequest Properties
| Property | Type | Description |
|---|---|---|
url | String | The URL to fetch JSON from |
method | Method | HTTP method (get, post, put, delete) |
headers | Map<String, dynamic>? | HTTP headers (e.g., Authorization) |
queryParameters | Map<String, dynamic>? | URL query parameters |
body | dynamic | Request body for POST/PUT |
contentType | String? | Content-Type header (e.g., application/json) |
Example
POST Request Example
When to Use
- ✅ Custom API endpoints
- ✅ Server-side rendering
- ✅ Dynamic content based on user data
- ✅ Multi-tenant applications
Comparison
| Method | Source | Best For | Network Required |
|---|---|---|---|
Stac(routeName:) | Stac Cloud | Production SDUI apps | ✅ Yes |
Stac.fromJson() | In-memory JSON | Testing, prototyping | ❌ No |
Stac.fromAssets() | Bundled JSON file | Offline, static content | ❌ No |
Stac.fromNetwork() | Custom API endpoint | Custom backends, advanced use cases | ✅ Yes |
Best Practices
- Always provide loading states: Users should know content is loading.
- Handle errors gracefully: Show meaningful error messages and retry options.
- Use appropriate method: Choose the rendering method that fits your use case.
- Validate JSON: Ensure your JSON follows Stac schema before rendering.