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
1 |
import 'package:flutter/material.dart'; |
2. Create void main runApp() method and here we would call the our main Parent MyApp class.
1 |
void main() => runApp(MyApp()); |
3. Create our main Root class named as MyApp extends StatelessWidget. Here we would call our Alert() class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar( title: Text('Call a Function Everytime When App Starts')), body: Center( child: Alert() ) ) ); } } |
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.
1 2 3 4 5 |
class Alert extends StatefulWidget { AlertState createState() => AlertState(); } |
5. Create our main class named as AlertState extends State.
1 2 3 4 |
class 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.
1 2 3 4 5 |
@override void initState() { showAlert(context); super.initState(); } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
Future showAlert(BuildContext context) async { await Future.delayed(Duration(seconds: 6)); showDialog( context: context, builder: (BuildContext context) { return AlertDialog( title: new Text('Welcome To Our App :) .'), actions: <Widget>[ FlatButton( child: new Text("OK"), onPressed: () { Navigator.of(context).pop(); }, ), ], ); }, ); } |
8. Creating a Text widget in Widget build area in AlertState class.
1 2 3 4 5 6 7 8 9 |
@override Widget build(BuildContext context) { return Scaffold( body: Center( child: Text('Call A Function Automatically When App Starts Everytime', style: TextStyle(fontSize: 22,), textAlign: TextAlign.center,) ), ); } |
9. Complete source code for main.dart file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar( title: Text('Call a Function Everytime When App Starts')), body: Center( child: Alert() ) ) ); } } class Alert extends StatefulWidget { AlertState createState() => AlertState(); } class AlertState extends State { @override void initState() { showAlert(context); super.initState(); } Future showAlert(BuildContext context) async { await Future.delayed(Duration(seconds: 6)); showDialog( context: context, builder: (BuildContext context) { return AlertDialog( title: new Text('Welcome To Our App :) .'), actions: <Widget>[ FlatButton( child: new Text("OK"), onPressed: () { Navigator.of(context).pop(); }, ), ], ); }, ); } @override Widget build(BuildContext context) { return Scaffold( body: Center( child: Text('Call A Function Automatically When App Starts Everytime', style: TextStyle(fontSize: 22,), textAlign: TextAlign.center,) ), ); } } |
Screenshots:
