|
| 1 | +--- |
| 2 | +title: Custom Actions |
| 3 | +sidebar_position: 2 |
| 4 | +--- |
| 5 | + |
| 6 | +# Custom Actions |
| 7 | + |
| 8 | +Custom Actions in FlutterFlow differ from custom functions in that they always return a `Future`. |
| 9 | +This makes them particularly useful for complex operations that may take time to complete, such |
| 10 | +as querying a database or calling a function that returns results after a delay. Additionally, |
| 11 | +Custom Actions are beneficial when you want to add a third-party dependency from `pub.dev`, |
| 12 | +allowing you to extend the capabilities of your application with external packages. |
| 13 | + |
| 14 | +:::tip[What is a Future?] |
| 15 | +Futures in **Flutter** represent an asynchronous operation that will return a value or an error at |
| 16 | +some point in the future. `Future<T>` indicates that the future will eventually provide a value of |
| 17 | +type `T`. So if your return value is a `String`, then the Custom Action will return |
| 18 | +a `Future<String>`, and the `String` return value will be output at some point in the future. |
| 19 | +::: |
| 20 | + |
| 21 | +## Key Use Cases |
| 22 | + |
| 23 | +- **Database Queries:** Perform complex queries to retrieve or update data in a database. |
| 24 | +- **API Calls:** Make asynchronous HTTP requests to external APIs and handle the responses. |
| 25 | +- **File Operations:** Manage file reading or writing operations that require time to complete. |
| 26 | +- **Third-Party Integrations:** Incorporate external packages and dependencies to enhance |
| 27 | + functionality, such as an external analytics package. |
| 28 | + |
| 29 | + |
| 30 | + |
| 31 | +## Using a Custom Action |
| 32 | + |
| 33 | +Once your Action code is finalized, saved, and compiled, you can start using this action as a part |
| 34 | +of your Action flow. |
| 35 | + |
| 36 | +In the following example, we have a Custom Action called `executeSearch` that takes an argument |
| 37 | +`searchItem` that is the search string from the search **TextField** of an ecommerce |
| 38 | +app's `HomePage`. |
| 39 | + |
| 40 | +<div style={{ |
| 41 | + position: 'relative', |
| 42 | + paddingBottom: 'calc(56.67989417989418% + 41px)', // Keeps the aspect ratio and additional padding |
| 43 | + height: 0, |
| 44 | + width: '100%' |
| 45 | +}}> |
| 46 | + <iframe |
| 47 | + src="https://demo.arcade.software/ZwlkhlPX867DW6cPQxKk?embed&show_copy_link=true" |
| 48 | + title="" |
| 49 | + style={{ |
| 50 | + position: 'absolute', |
| 51 | + top: 0, |
| 52 | + left: 0, |
| 53 | + width: '100%', |
| 54 | + height: '100%', |
| 55 | + colorScheme: 'light' |
| 56 | + }} |
| 57 | + frameborder="0" |
| 58 | + loading="lazy" |
| 59 | + webkitAllowFullScreen |
| 60 | + mozAllowFullScreen |
| 61 | + allowFullScreen |
| 62 | + allow="clipboard-write"> |
| 63 | + </iframe> |
| 64 | +</div> |
| 65 | + |
| 66 | +## Using the Custom Action Result |
| 67 | + |
| 68 | +In our previous example, we enabled the **Return Value** of the Custom Action to return a |
| 69 | +`List<Product>` when the search keyword is valid. With this change the code will change from |
| 70 | + |
| 71 | +``` |
| 72 | +Future executeSearch(String searchItem) async { |
| 73 | + // Add your function code here! |
| 74 | +} |
| 75 | +``` |
| 76 | + |
| 77 | +to |
| 78 | + |
| 79 | +``` |
| 80 | +Future<List<ProductStruct>> executeSearch(String searchItem) async { |
| 81 | +// Add your function code here! |
| 82 | +} |
| 83 | +``` |
| 84 | + |
| 85 | +Let's modify our Action Flow now so we can use the custom action result values within our Action |
| 86 | +Flow. |
| 87 | + |
| 88 | + |
| 89 | +<div style={{ |
| 90 | + position: 'relative', |
| 91 | + paddingBottom: 'calc(56.67989417989418% + 41px)', // Keeps the aspect ratio and additional padding |
| 92 | + height: 0, |
| 93 | + width: '100%' |
| 94 | +}}> |
| 95 | + <iframe |
| 96 | + src="https://demo.arcade.software/Phny5irmH6G2A2TJili0?embed&show_copy_link=true" |
| 97 | + title="" |
| 98 | + style={{ |
| 99 | + position: 'absolute', |
| 100 | + top: 0, |
| 101 | + left: 0, |
| 102 | + width: '100%', |
| 103 | + height: '100%', |
| 104 | + colorScheme: 'light' |
| 105 | + }} |
| 106 | + frameborder="0" |
| 107 | + loading="lazy" |
| 108 | + webkitAllowFullScreen |
| 109 | + mozAllowFullScreen |
| 110 | + allowFullScreen |
| 111 | + allow="clipboard-write"> |
| 112 | + </iframe> |
| 113 | +</div> |
| 114 | + |
| 115 | +<p></p> |
| 116 | + |
| 117 | +:::tip[LOOKING for other CUSTOM action properties?] |
| 118 | +To learn more about Custom Action settings, such as the Exclude From Compilation toggle, Include |
| 119 | +Build Context toggle, and other properties like Callback Actions, please check out this |
| 120 | +[**comprehensive guide**](custom-code.md). |
| 121 | +::: |
| 122 | + |
| 123 | + |
| 124 | + |
| 125 | + |
| 126 | + |
| 127 | + |
0 commit comments