Skip to main content
While Stac provides various built-in actions (navigation, dialogs, network requests, etc.), you may need to create custom actions for your specific use cases. This guide walks you through creating custom StacAction classes that work seamlessly with Stac’s JSON serialization and parser system.

What is a Custom StacAction?

A custom StacAction is a Dart class that:
  • Extends the StacAction base class
  • Can be serialized to and deserialized from JSON
  • Works with Stac’s action parser system to execute behaviors
  • Can be triggered from widgets in your JSON definitions
Custom actions enable you to:
  • Integrate third-party services and APIs
  • Implement business-specific logic
  • Extend Stac’s action functionality beyond built-in actions
  • Create reusable behaviors for your application

Prerequisites

Before creating a custom action, ensure you have:
  1. Dependencies: stac and json_annotation packages
  2. Code Generation: build_runner for generating JSON serialization code
  3. Parser: A corresponding action parser to execute your action.

Step-by-Step Guide

Step 1: Define Your Action Class

Create a new file (e.g., lib/actions/stac_share_action.dart) and define your action class:

Step 2: Required Components

Every custom StacAction must include:

1. Part File Declaration

This enables code generation for JSON serialization.

2. JsonSerializable Annotation

For nested actions or complex types, use explicitToJson: true:

3. Action Type Getter

This unique identifier is used in JSON: {"actionType": "share"}.

4. Constructor

The base StacAction constructor handles JSON data internally.

5. fromJson Factory Constructor

6. toJson Method

Step 3: Generate Code

Run code generation to create the *.g.dart file:
This generates stac_share_action.g.dart with the serialization logic.

Step 4: Create an Action Parser

To execute your action, create an action parser:

Step 5: Register the Action Parser

Register your action parser during Stac initialization:

Advanced Patterns

Using Converters

Similar to widgets, actions can use converters for special types:

DoubleConverter

For double fields that may come as integers in JSON:

Using Custom Actions

In Dart (stac/ folder)
After stac build or stac deploy, your generated json looks like this:
Actions are commonly used in widget onPressed, onTap, and other callback properties. For example: