# Set App State

{% embed url="<https://youtu.be/SFkOFCakK9U?si=sawt-d0RrRSJGWmC>" %}

Watch this on Youtube: <https://www.youtube.com/watch?v=SFkOFCakK9U>

The **Set App State** action allows you to update the global application state. This is useful for managing state that affects the entire app or multiple components, enabling centralized control over app-wide data.

## Properties

| Property        | Type  | Required | Description                                                                                                                       |
| --------------- | ----- | -------- | --------------------------------------------------------------------------------------------------------------------------------- |
| `State Updates` | Array | Yes      | List of state updates. Each update contains a State Name (dropdown) and Value pair. Use "Add New" button to add multiple updates. |

## Use Cases

* **User Authentication**: Manage login status and user data globally
* **Theme Management**: Control app-wide theme and appearance settings
* **Language Settings**: Store user language preferences across the app
* **Shopping Cart**: Maintain cart state across different pages
* **App Configuration**: Store feature flags and configuration settings
* **User Preferences**: Save user choices and settings globally

## Default Behavior

By default:

| Behavior                  | Description                                           |
| ------------------------- | ----------------------------------------------------- |
| **Global Scope**          | Updates affect all parts of the app                   |
| **Persistence**           | State changes persist until app restart               |
| **No UI Rebuild**         | UI updates only when components subscribe to changes  |
| **Multiple Updates**      | All state updates are applied in sequence             |
| **Expression Evaluation** | All `Value` expressions are evaluated before updating |

## How to Use

<figure><img src="https://3626461507-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FbhApDTL7kHrXte2TTtjs%2Fuploads%2Fgit-blob-abee3e373fdb955e64ca092780bf0f5143b862df%2Fset_app_state_action.png?alt=media" alt="Call External Method action configuration showing message name and payload fields"><figcaption><p>Call External Method action configuration in Digia Studio</p></figcaption></figure>

1. Attach the action to a widget event (login, settings change, etc.)
2. Select **Set App State** from the action list
3. In the `State Updates` section, select a `State Name` from the dropdown for each update
4. Set the corresponding `Value` for each state variable
5. Click **Add New** to add additional state updates as needed

## Implementation Details

The action uses `DUIAppState().update()` to modify global application state. Changes are immediately available throughout the app and persist until the app is restarted.

State values can be any type (strings, numbers, booleans, objects, arrays) and support expressions for dynamic evaluation.

Example native code usage:

```dart
// Update single values
DUIAppState().update('userName', 'John Doe');
DUIAppState().update('isLoggedIn', true);

// Update complex objects
DUIAppState().update('userProfile', {
  'name': 'John Doe',
  'email': 'john@example.com'
});
```

## Related Actions

* [Set State](https://docs.digia.tech/logic-and-interaction/actions/set-state) - Update page or component-specific state
* [Rebuild State](https://docs.digia.tech/logic-and-interaction/actions/rebuild-state) - Trigger UI rebuilds for state changes
* [Execute Callback](https://docs.digia.tech/logic-and-interaction/actions/execute-callback) - Handle state change callbacks

***
