|
| 1 | +--- |
| 2 | +title: Component Lifecycle |
| 3 | +sidebar_position: 3 |
| 4 | +toc_max_heading_level: 5 |
| 5 | +--- |
| 6 | + |
| 7 | +# Component Lifecycle |
| 8 | + |
| 9 | +In FlutterFlow, knowing component lifecycle is crucial for managing state and optimizing your |
| 10 | +app's performance. |
| 11 | + |
| 12 | +Let's delve into the key moments in the lifecycle of a **Component**: |
| 13 | + |
| 14 | +- **Creation**: Component instances are created dynamically when they are utilized within a page |
| 15 | + or another component. This means that component instances are created as needed, which helps in |
| 16 | + managing resources efficiently and avoiding unnecessary overhead. |
| 17 | + |
| 18 | +- **Initialization:** Actions defined in the `On Initialization` **Action Trigger** are executed |
| 19 | + during this phase. For instance, you can initialize local state variables with initial values, or |
| 20 | + start component animations in this phase. |
| 21 | + At this stage, component state variables with their default values (if any) are also created. |
| 22 | + These variables hold data specific to the component, such as form inputs or toggle states, and are |
| 23 | + essential for managing the component’s internal state. |
| 24 | + |
| 25 | +- **Updating:** While in use, the component can receive updated parameters from its parent when |
| 26 | + the parent rebuilds itself, allowing the component to adjust its behavior and appearance |
| 27 | + accordingly. When updating state variables inside a component, you can choose to rebuild only the |
| 28 | + component itself or the entire page containing the given component. This dynamic updating is |
| 29 | + crucial for maintaining a responsive and interactive user experience. |
| 30 | + |
| 31 | +- **Disposal:** When the component is no longer needed, such as when a user navigates away |
| 32 | + from the page or the component is explicitly removed, it is destroyed. |
| 33 | + |
| 34 | +In FlutterFlow, most of these lifecycle stages are handled internally by FlutterFlow's architecture. |
| 35 | +However, we expose some of the methods so that you, as a developer, can decide what additional |
| 36 | +configurations to load upon initialization and when to re-render the UI based on interactions. |
| 37 | + |
| 38 | +Let's read more about them in the following sections: |
| 39 | + |
| 40 | +## Initialization Action Triggers |
| 41 | + |
| 42 | +During the initialization of a **Component**, FlutterFlow exposes the `On Initialization` **Action |
| 43 | +Trigger** that assist you in loading resources or initializing data when the Component is loaded in |
| 44 | +a Page or a Component. |
| 45 | + |
| 46 | +:::info[What are Action Triggers?] |
| 47 | +**Action Triggers** serve as event listeners or handlers that respond to |
| 48 | +specific events or user interactions within an application. FlutterFlow provides |
| 49 | +developers with a way to define logic that responds to various events, such as |
| 50 | +button clicks, page loads, form submissions, or data changes. |
| 51 | +To learn more, head over to **[Action Flow Editor](#)** section. |
| 52 | +::: |
| 53 | + |
| 54 | +As you open the [Action Flow Editor](#) for your Component, you can see the `On Initialization` |
| 55 | +**Action Trigger** exposed for your **Component**. |
| 56 | + |
| 57 | +### On Initialization [Action Trigger] |
| 58 | + |
| 59 | +The `On Initialization` action trigger in FlutterFlow allows you to define actions that should occur |
| 60 | +when a component loads or initializes, such as setting up necessary data, state variables, or other |
| 61 | +initialization tasks. |
| 62 | + |
| 63 | +If the component stops being shown in the UI and then becomes visible again, |
| 64 | +the actions under the **On Initialization** action trigger will be re-triggered so any |
| 65 | +setup tasks are re-executed. For dynamically generated components, such as those in a ListView with |
| 66 | +a query, each instance will trigger the actions under `On Initialization` action trigger when it is |
| 67 | +created. |
| 68 | + |
| 69 | +## Component state |
| 70 | + |
| 71 | +:::note[STATE VARIABLES] |
| 72 | +A state variable holds information or data about your UI at any given moment. To learn more |
| 73 | +about **states and state management, [refer here.](#)** |
| 74 | +::: |
| 75 | + |
| 76 | +**Component state** refers to the information that a component tracks about its current condition or |
| 77 | +the data it manages internally. This can include data such as whether a button is enabled, the value |
| 78 | +of a slider, or the entries in a dynamically updated list. Component state variables are only |
| 79 | +accessible within the given component's scope. |
| 80 | + |
| 81 | +This type of variable is particularly useful for storing data that affects how the component behaves |
| 82 | +or appears, such as toggling UI elements, keeping track of user choices within the component, or |
| 83 | +caching data pertinent to the component's functionality. |
| 84 | + |
| 85 | +For example, |
| 86 | + |
| 87 | +- In a custom drop-down menu component, you might use a component state variable to keep track of |
| 88 | + which item is currently selected. |
| 89 | +- In a toggle switch component, you could use a component state variable to store the on/off |
| 90 | + state based on user interaction. |
| 91 | + This approach ensures that the state of the component is maintained as it interacts with the user |
| 92 | + or other parts of the application. |
| 93 | + |
| 94 | +When the value of a component state variable is changed, the component can be re-rendered with the |
| 95 | +updated values, displaying the latest state of the component with these updates. |
| 96 | + |
| 97 | +### Creating a Component State |
| 98 | + |
| 99 | +To create a new **Component State variable** in your component, follow the steps: |
| 100 | + |
| 101 | + |
| 102 | +<div style={{ |
| 103 | + position: 'relative', |
| 104 | + paddingBottom: 'calc(56.67989417989418% + 41px)', // Keeps the aspect ratio and additional padding |
| 105 | + height: 0, |
| 106 | + width: '100%' |
| 107 | +}}> |
| 108 | + <iframe |
| 109 | + src="https://demo.arcade.software/nEmCDqupF7YHUTi4hKvW?embed&show_copy_link=true" |
| 110 | + title="Create Component State" |
| 111 | + style={{ |
| 112 | + position: 'absolute', |
| 113 | + top: 0, |
| 114 | + left: 0, |
| 115 | + width: '100%', |
| 116 | + height: '100%', |
| 117 | + colorScheme: 'light' |
| 118 | + }} |
| 119 | + frameborder="0" |
| 120 | + loading="lazy" |
| 121 | + webkitAllowFullScreen |
| 122 | + mozAllowFullScreen |
| 123 | + allowFullScreen |
| 124 | + allow="clipboard-write"> |
| 125 | + </iframe> |
| 126 | +</div> |
| 127 | + |
| 128 | +While creating a Component State, the following properties are included: |
| 129 | + |
| 130 | +- **Is List:** This property determines whether the variable can hold multiple values of the same |
| 131 | + data type (like a list or array) or just a single value. |
| 132 | + |
| 133 | +- **Initial Field Value:** This property sets the default value for the variable when it is first |
| 134 | + created. It's like setting the starting point or the value that the variable begins with before |
| 135 | + anything else happens. |
| 136 | + |
| 137 | +- **Nullable:** This property determines whether the variable can have a null value. When "**Nullable**" is set to true, it means the variable can be empty or have a null value. This is |
| 138 | + useful when dealing with optional data or scenarios where the absence of a value is valid. |
| 139 | + |
| 140 | +Now, let's apply these concepts to the `isFavourite` variable in the context of the above example: |
| 141 | + |
| 142 | +- For the `isFavourite` variable, it is a single value (boolean), so **Is List** would be set to |
| 143 | + false. |
| 144 | + |
| 145 | +- The **Initial Field Value** would also be set to **false**, indicating that the item is not |
| 146 | + favorited by default. |
| 147 | + |
| 148 | +- **Nullable** property will be set to false, as the variable should always have a boolean value |
| 149 | + (true or false) and never be null. |
| 150 | + |
| 151 | +:::note |
| 152 | +You can set the **Data Type** of your Component State variable to any primitive data types such as **String, Integer, Boolean, Double** or to any other complex built-in data types such as **Enum, Custom Data |
| 153 | +Type, Document,** etc. To learn more about the available data types, refer the [**Data |
| 154 | +Representation Section.** ](#) |
| 155 | +::: |
| 156 | + |
| 157 | +### Get Component State Value |
| 158 | + |
| 159 | +In the following example, we demonstrate how to toggle the heart icon from an outlined to a filled |
| 160 | +icon based on the `isFavourite` state variable. We introduce a `Conditional Builder` widget that |
| 161 | +allows us to show a widget tree based on **If/Else If/Else** conditions. The goal is to visually |
| 162 | +indicate whether a product has been favorited by the user. |
| 163 | + |
| 164 | +Follow the steps as below: |
| 165 | + |
| 166 | +<div style={{ |
| 167 | + position: 'relative', |
| 168 | + paddingBottom: 'calc(56.67989417989418% + 41px)', // Keeps the aspect ratio and additional padding |
| 169 | + height: 0, |
| 170 | + width: '100%' |
| 171 | +}}> |
| 172 | + <iframe |
| 173 | + src="https://demo.arcade.software/Y96decdgYWVll3SP9Jk8?embed&show_copy_link=true" |
| 174 | + title="Get Component State" |
| 175 | + style={{ |
| 176 | + position: 'absolute', |
| 177 | + top: 0, |
| 178 | + left: 0, |
| 179 | + width: '100%', |
| 180 | + height: '100%', |
| 181 | + colorScheme: 'light' |
| 182 | + }} |
| 183 | + frameborder="0" |
| 184 | + loading="lazy" |
| 185 | + webkitAllowFullScreen |
| 186 | + mozAllowFullScreen |
| 187 | + allowFullScreen |
| 188 | + allow="clipboard-write"> |
| 189 | + </iframe> |
| 190 | +</div> |
| 191 | + |
| 192 | +### Update Component State [Action] |
| 193 | + |
| 194 | +**Component state** values can only be updated via actions. Whenever you want to update the |
| 195 | +component state, call an action called **Update Component State** from the Action Flow Editor |
| 196 | +of the component. |
| 197 | + |
| 198 | +In the following demo, we open the Action Flow Editor on the parent widget `Conditional Builder` and |
| 199 | +call the **Update Component State** action to toggle the value of `isFavourite`. |
| 200 | + |
| 201 | +<div style={{ |
| 202 | + position: 'relative', |
| 203 | + paddingBottom: 'calc(56.67989417989418% + 41px)', // Keeps the aspect ratio and additional padding |
| 204 | + height: 0, |
| 205 | + width: '100%' |
| 206 | +}}> |
| 207 | + <iframe |
| 208 | + src="https://demo.arcade.software/Y96decdgYWVll3SP9Jk8?embed&show_copy_link=true" |
| 209 | + title="Get Component State" |
| 210 | + style={{ |
| 211 | + position: 'absolute', |
| 212 | + top: 0, |
| 213 | + left: 0, |
| 214 | + width: '100%', |
| 215 | + height: '100%', |
| 216 | + colorScheme: 'light' |
| 217 | + }} |
| 218 | + frameborder="0" |
| 219 | + loading="lazy" |
| 220 | + webkitAllowFullScreen |
| 221 | + mozAllowFullScreen |
| 222 | + allowFullScreen |
| 223 | + allow="clipboard-write"> |
| 224 | + </iframe> |
| 225 | +</div> |
| 226 | + |
| 227 | +#### Rebuild on Update |
| 228 | + |
| 229 | +When updating your component state in FlutterFlow, you'll often come across the **Update |
| 230 | +Type** property in your Action properties. Here's what it means: |
| 231 | + |
| 232 | +- **Rebuild Containing Page:** This option triggers a re-rendering of the page |
| 233 | + containing this component. |
| 234 | + |
| 235 | +- **Rebuild Current Component:** This option triggers a re-rendering of the current component only. |
| 236 | + |
| 237 | +- **No Rebuild:** Choose this option when you need to update the state value without |
| 238 | + immediately reflecting the changes in the UI. |
| 239 | + |
| 240 | +:::warning[Expensive Rebuilds] |
| 241 | +Too many rebuilds can impact performance because rebuilding the widget tree |
| 242 | +frequently consumes resources and may lead to decreased responsiveness and |
| 243 | +increased battery usage. Therefore, it's essential to consider the trade-offs |
| 244 | +and use rebuilds judiciously to maintain optimal app performance. |
| 245 | + |
| 246 | +To learn more about what happens behind the scenes, refer to |
| 247 | +the [**Generated Page**](#) section. |
| 248 | +::: |
| 249 | + |
| 250 | + |
| 251 | + |
| 252 | + |
| 253 | + |
0 commit comments