FlutterFlow offers a powerful way to execute code globally during your app’s startup and initialization process using Initial Actions and Final Actions. These actions are inserted into the main()
function and allow you to run custom logic before or after your app setup.
What Are Initial and Final Actions?
- Initial Actions: Executed at the very start of the
main()
function, before the app is initialized or any widget is rendered. - Final Actions: Executed at the end of the
main()
function, just before the app runs withrunApp()
.
These are useful for performing tasks like setting up services, initializing configurations, or logging system information.
Where to Find This in FlutterFlow
You can access this from:
Custom Code → Configuration Files → main.dart
There you’ll see two sections labeled:
- Initial Actions
- Final Actions
Here’s a screenshot for reference:
Creating a Custom Action for main.dart
To use this feature, you must create a Custom Action without any input parameters. This is important because Initial and Final Actions in main.dart
do not support user input or context.
Steps:
- Go to
Custom Code → Custom Actions → + Add Custom Action
- Write your Dart code
- Leave the input section empty
Example:
dart
Future<void> initializeFirebase() async {
await Firebase.initializeApp();
}
Once created, you can assign this action to either the Initial or Final section.
When to Use Initial vs Final Actions
Type | Runs When | Use Cases |
---|---|---|
Initial Action | At the beginning of main() |
Firebase setup, config loading, secure storage |
Final Action | At the end of main() |
Logging, readiness tracking, timers |
Tips and Best Practices
- Use only stateless logic (no context or user input).
- Keep your actions short and reliable, as long delays can slow app launch.
- You can add multiple actions in both sections to organize your setup steps.
Summary
Initial and Final Actions are a great way to manage global startup and cleanup logic in a clean, centralized way - without modifying main.dart
manually.
If you’re already using this feature, feel free to share your use cases or tips. It helps others learn from your experience.
Let me know if you’d like this content in Markdown or need it customized for a specific developer forum.