Constants are used to define values that remain unchanged throughout the lifetime of an application. Using constants is a good practice for values that do not need to be recalculated or reassigned.

Here are some real-world examples where using constants is beneficial:

  1. Educational Apps: Constants for course-related data like the number of questions in a quiz or passing grade percentage.

  2. Fitness Apps: Standard values like target heart rate zones or calorie burn rates based on specific exercises.

  3. Game Development: Constants for game settings like default player health, speed, or levels. For example, const int maxHealth = 100;.

How are Constants different from the App state variables?

Constants don't change. Once you set its value, you can't change it from within the app. On the other hand, app state variables are dynamic. They can change and manage the app's changing data, like user status or preference.

Create and use Constants

Let's see how you can create and use a constant in your app.

  1. Navigate to the App Values (from the navigation menu) > Constants.

  2. Click on the + Add App Constant. A popup will open.

    1. Provide the Constant Name.

    2. Click on the dropdown list below and select the suitable Data Type.

    3. Optional: enable the Is List to store multiple values of the same data type, such as a list of fruit names.

    4. Click Create.

    5. Set the constant Value.

  3. Now you can access the constants via Set from Variable menu > Constants > Project Constants > [your constant name].

Supported data types

The following data types are available for App State:

  • Integer: To store numbers such as 100, 4302, 50000, etc.

  • Double: To store a decimal number such as 12.43, 3233.50, 65.5666, etc.

  • String: To store plain text such as title, description, etc.

  • Boolean: To store either a true or false value.

  • Color: To store the color value.

  • Image Path: To store the URL of the uploaded image.

  • Video Path: To store the URL of the uploaded video.

  • Audio Path: To store the URL of the uploaded audio.

Published Date: November 24, 2023

Last updated