Skip to content

Commit 717fbaf

Browse files
committed
Code Expression
1 parent cba8993 commit 717fbaf

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed
Binary file not shown.

docs/resources/control-flow/functions/utility-functions.md

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ To add inline function, open the Set from Variable dialog wherever it's possible
100100

101101
For example, we may want to quickly calculate the discount amount of a product where the discount is 18% of the MRP of the product. The expression would be `cost - (cost * discount)`.
102102

103+
:::tip
104+
Looking for more power and flexibility? Use a new [**Code Expression**](#code-expression). It’s a more advanced version of Inline Functions that lets you access FlutterFlow generated resources without passing them as arguments. You also get real-time autocomplete and inline error checking for faster, more accurate logic.
105+
:::
106+
103107
**Precedence of operations**
104108

105109
Inline Function for math operations follow typical precedence (e.g., multiplication/division before addition/subtraction), but parentheses can change the order.
@@ -162,6 +166,74 @@ Here are some common expressions you can use for your business logic:
162166
| `int.parse(s)` | Convert the **String** into an **integer.** | `int.parse(stringValue)` | `int` |
163167

164168

169+
## Code Expression
170+
171+
**Code Expression** lets you write short Dart code directly in widget property fields and action flows in FlutterFlow. It’s a more powerful version of [**Inline Function**](#inline-function-code-expressions), allowing you to directly access FlutterFlow generated classes, global variables, widget properties, parameters, and more without needing to manually pass them as inputs.
172+
173+
Code Expressions also support real-time autocomplete, making it easy to discover available fields as you type. For example, when you type `FFAppState().`, it will suggest all available app state variables along with their types.
174+
175+
In addition, inline validation provides immediate feedback as you write, helping you catch syntax errors or invalid property references.
176+
177+
:::tip
178+
- To explore what you can access within a code expression, refer to the [**Common Examples**](../../../ff-concepts/adding-customization/common-examples.md) page.
179+
- Press `^ + Space` (or `Ctrl + Space`) while typing to see suggestions for what you can access in your code expression.
180+
- You can access values inside custom structs. For example, you can use `FFAppState().localDeviceInfo.osVersion` if that field exists in your app state.
181+
- To use code expressions better, it's helpful to understand how FlutterFlow builds your project behind the scenes. You can check the [**State Management**](../../../generated-code/state-mgmt-gen-code.md) page and other **Generated Code** sections to learn how everything is set up.
182+
183+
:::
184+
185+
Here are a couple of examples showing how to access App State and Page State within a code expression:
186+
187+
- **App State Access:** For example, to check if dark mode is enabled using an App State variable:
188+
189+
```jsx
190+
FFAppState().enableDarkMode ? 'Dark Mode On' : 'Light Mode Off'
191+
```
192+
193+
This accesses the global `enableDarkMode` boolean stored in `FFAppState`, and returns a string based on its value.
194+
195+
- **Page and Component State Access:** For example, to access a page or component state variable like `searchText`, you start with `_model.` and then select the variable from the autocomplete suggestions.
196+
197+
```jsx
198+
_model.searchText.isEmpty ? '' : 'Searching for "${_model.searchText}"'
199+
```
200+
201+
This expression checks if the `searchText` variable (defined as a page state) is empty, and returns an appropriate message. The `_model` object refers to the current page’s generated state model.
202+
203+
Here's an example of adding a Code Expression:
204+
205+
<div style={{
206+
position: 'relative',
207+
paddingBottom: 'calc(56.67989417989418% + 41px)', // Keeps the aspect ratio and additional padding
208+
height: 0,
209+
width: '100%'}}>
210+
<iframe
211+
src="https://demo.arcade.software/6RV03v2ByVqRgCTeg3QF?embed&show_copy_link=true"
212+
title=""
213+
style={{
214+
position: 'absolute',
215+
top: 0,
216+
left: 0,
217+
width: '100%',
218+
height: '100%',
219+
colorScheme: 'light'
220+
}}
221+
frameborder="0"
222+
loading="lazy"
223+
webkitAllowFullScreen
224+
mozAllowFullScreen
225+
allowFullScreen
226+
allow="clipboard-write">
227+
</iframe>
228+
</div>
229+
<p></p>
230+
231+
### Execute Custom Code [Action]
232+
233+
To use a Code Expression when triggering actions in FlutterFlow (i.e., inside an Action Flow), you can use the **Execute Custom Code** action. This allows you to run a Dart expression when something happens, such as tapping a button or after a page loads.
234+
235+
![execute-custom-code.avif](img/execute-custom-code.avif)
236+
165237
166238
## Custom Functions
167239
@@ -170,3 +242,18 @@ You can also use custom functions to handle slightly more complex calculations o
170242
:::info
171243
Learn more about [**Custom Functions**](../../../ff-concepts/adding-customization/custom-functions.md).
172244
:::
245+
246+
## FAQS
247+
<details>
248+
<summary>
249+
How is a Code Expression different from an Inline Function?
250+
</summary>
251+
<p>
252+
Code Expressions are a more advanced and flexible version of **Inline Functions.
253+
254+
With Inline Functions, you had to manually pass values as arguments. In contrast, Code Expressions let you directly reference FlutterFlow generated resources (such as `FFAppState()`, `_model`, context, and more) without needing to pass them in.
255+
256+
You can write any valid Dart expression in a code expression, even multi-line logic using anonymous functions. Plus, Code Expressions support real-time autocomplete and inline error validation, making it much easier to discover available variables and avoid mistakes.
257+
258+
</p>
259+
</details>

0 commit comments

Comments
 (0)