What is a Stac Action Parser?
A Stac action parser is a custom class that interprets specific JSON objects representing actions and executes the corresponding logic in your Flutter application. This allows for highly flexible and customizable behavior tailored to the specific needs of your application.Creating a Custom Action Parser
To create a custom action parser, you need to follow these steps:- Define the JSON Structure: Define the structure of the JSON object that your action parser will interpret. This structure should be well-documented and easy to understand.
- Create the Action Parser Class: Create a new Dart class that implements the
StacActionParser
interface provided by Stac. This class will contain the logic to interpret the JSON object and execute the corresponding action. - Register the Action Parser: Register the custom action parser with Stac so that it can be used to interpret JSON objects.
Example Action Parser
In this example, we will create a custom action parser that calls a print function with a provided message.Step 1: Define the JSON Structure
The JSON structure for the custom action might look like this:fromJson
method to convert the JSON object to the custom action object.
Step 2: Create the Action Parser Class
Next, we create a custom action parser class that implements theStacActionParser
interface. This class will interpret the JSON object and execute the corresponding logic.
Step 3: Register the Action Parser
Finally, you need to register the custom action parser with Stac. There are 2 ways to register the custom action parser:- Register in
Stac.initialize
: You can register the parser when initializing Stac by passing it in theactionParser
parameter.
- Register through StacRegistry: You can also register the action parser anywhere using the
StacRegistry
class.
StacRegistry
provides you with two method to register the action parser.
- Register a single action parser:
- Register multiple action parsers: