Flutter Call A Function Automatically When App Starts Everytime

In flutter the StatefulWidget provides us a method named as initState() which is executed every single time when flutter app’s starts. The initState() method executed every time when a object is inserted into View class tree. This method will class once for each State object is created for example if we have multiple StatefulWidget classes then we can call this method multiple times and if we have single StatefulWidget class then we can call this method single time. So in this tutorial we would Flutter Call A Function Automatically When App Starts Everytime Android iOS example tutorial.

Contents in this project Flutter Call A Function Automatically When App Starts Everytime Android iOS Example Tutorial:

1. Import material.dart package in your app’s main.dart file

2. Create void main runApp() method and here we would call the our main Parent MyApp class.

3. Create our main Root class named as MyApp extends StatelessWidget. Here we would call our Alert() class.

4. Create a class named as Alert extends StatefulWidget. In this class we would make the createState() method and call the AlertState() class. The createState() method would enable the mutable state management in given class tree.

5. Create our main class named as AlertState extends State.

6. Now we would override the inbuilt initState() method by creating new one. Inside the function we would call a function in which we are displaying a Welcome alert dialog message.

7. Creating a Future showAlert() function with Build Context argument. Inside the function first we would use Await method to pause the code executing for 6 seconds like making a delay then show the welcome alert dialog message on screen.

8. Creating a Text widget in Widget build area in AlertState class.

9. Complete source code for main.dart file:

Screenshots:

Flutter Call A Function Automatically When App Starts Everytime

Leave a Reply

Your email address will not be published.