Skip to content

Commit 6755b6a

Browse files
authored
Merge pull request #394 from FlutterFlow/feature/code-expression
Code Expression
2 parents aa1d348 + 62ea810 commit 6755b6a

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed
Binary file not shown.

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

Lines changed: 92 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 the new [**Custom Code Expression**](#custom-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,79 @@ 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+
## Custom Code Expression
170+
171+
**Custom 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+
Custom 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+
:::info
178+
To use Custom Code Expression, you must have an active [**FlutterFlow paid plan**](https://www.flutterflow.io/pricing).
179+
:::
180+
181+
:::tip
182+
- To explore what you can access within a Custom code expression, refer to the [**Common Examples**](../../../ff-concepts/adding-customization/common-examples.md) page.
183+
- Press `^ + Space` (or `Ctrl + Space`) while typing to see suggestions for what you can access in your Custom code expression.
184+
- You can access values inside custom structs. For example, you can use `FFAppState().localDeviceInfo.osVersion` if that field exists in your app state.
185+
- To use Custom 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.
186+
187+
:::
188+
189+
Here are a couple of examples showing how to access App State and Page State within a Custom code expression:
190+
191+
- **App State Access:** For example, to check if dark mode is enabled using an App State variable:
192+
193+
```jsx
194+
FFAppState().enableDarkMode ? 'Dark Mode On' : 'Light Mode Off'
195+
```
196+
197+
This accesses the global `enableDarkMode` boolean stored in `FFAppState`, and returns a string based on its value.
198+
199+
- **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.
200+
201+
```jsx
202+
_model.searchText.isEmpty ? '' : 'Searching for "${_model.searchText}"'
203+
```
204+
205+
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.
206+
207+
Here's an example of adding a Custom Code Expression:
208+
209+
<div style={{
210+
position: 'relative',
211+
paddingBottom: 'calc(56.67989417989418% + 41px)', // Keeps the aspect ratio and additional padding
212+
height: 0,
213+
width: '100%'}}>
214+
<iframe
215+
src="https://demo.arcade.software/6RV03v2ByVqRgCTeg3QF?embed&show_copy_link=true"
216+
title=""
217+
style={{
218+
position: 'absolute',
219+
top: 0,
220+
left: 0,
221+
width: '100%',
222+
height: '100%',
223+
colorScheme: 'light'
224+
}}
225+
frameborder="0"
226+
loading="lazy"
227+
webkitAllowFullScreen
228+
mozAllowFullScreen
229+
allowFullScreen
230+
allow="clipboard-write">
231+
</iframe>
232+
</div>
233+
<p></p>
234+
235+
### Execute Custom Code [Action]
236+
237+
To use a Custom 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.
238+
239+
![execute-custom-code.avif](img/execute-custom-code.avif)
240+
241+
The Execute Custom Code action can be really helpful in scenarios where the home page is removed early from the navigation stack and standard navigation using the local context may fail. To prevent this, you can [use the global navigator context](../../../ff-concepts/navigation-routing/deep-dynamic-linking.md#using-global-context-to-navigate) inside a code expression.
165242
166243
## Custom Functions
167244
@@ -170,3 +247,18 @@ You can also use custom functions to handle slightly more complex calculations o
170247
:::info
171248
Learn more about [**Custom Functions**](../../../ff-concepts/adding-customization/custom-functions.md).
172249
:::
250+
251+
## FAQS
252+
<details>
253+
<summary>
254+
How is a Custom Code Expression different from an Inline Function?
255+
</summary>
256+
<p>
257+
Custom Code Expression is a more advanced and flexible version of Inline Function.
258+
259+
With Inline Functions, you had to manually pass values as arguments. In contrast, Custom Code Expressions let you directly reference FlutterFlow generated resources (such as `FFAppState()`, `_model`, context, and more) without needing to pass them in.
260+
261+
You can write any valid Dart expression in a Custom code expression, even multi-line logic using anonymous functions. Plus, Custom Code Expressions support real-time autocomplete and inline error validation, making it much easier to discover available variables and avoid mistakes.
262+
263+
</p>
264+
</details>

0 commit comments

Comments
 (0)