Flutter – initSate()
There are two types of widgets provided in Flutter.
- The Stateless Widget
- The Stateful Widget
As the name suggests Stateful Widgets are made up of some ‘States’. The initState() is a method that is called when an object for your stateful widget is created and inserted inside the widget tree. It is basically the entry point for the Stateful Widgets. initState() method is called only and only once and is used generally for initializing the previously defined variables of the stateful widget. initState() method is overridden mostly because as mentioned earlier it is called only once in its lifetime. If you want to trigger it again you have to shift the control to an entirely new screen and a new state. Let’s understand it more clearly with the example below.
Example Project
Create a class named “stateExample” that extends a Stateful Widget.
class stateExample extends StatefulWidget { @override State<stateExample> createState() => _stateExampleState(); } class _stateExampleState extends State<stateExample> { @override Widget build(BuildContext context) { return Container(); } }
Under the @override of the Stateful Widget call the initState() method
@override initState() { print("initState Called"); }
Complete the rest of the code according to your requirements.
Implementation:
Dart
import 'package:flutter/material.dart' ; void main() { runApp( const MyApp()); } class MyApp extends StatelessWidget { const MyApp({Key? key}) : super(key: key); @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar( title: const Text( "GeeksforGeeks" , ), backgroundColor: Colors.green, ), body: const stateExample(), ), ); } } // ignore: camel_case_types class stateExample extends StatefulWidget { const stateExample({Key? key}) : super(key: key); @override State<stateExample> createState() => _stateExampleState(); } // ignore: camel_case_types class _stateExampleState extends State<stateExample> { @override // ignore: must_call_super initState() { // ignore: avoid_print print( "initState Called" ); } @override Widget build(BuildContext context) { // ignore: avoid_print print( " Build method called" ); return Center( child: ElevatedButton( style: ButtonStyle( backgroundColor: MaterialStateProperty.all(Colors.green)), onPressed: () {}, child: const Text( 'initState Demonstration' , style: TextStyle(color: Colors.white), ), ), // RaisedButton widget is deprecated and should not be used anymore. // Use ElevatedButton instead. // child: RaisedButton( // color: Colors.green, // elevation: 10, // onPressed: () {}, // child: const Text( // 'initState Demonstration', // style: TextStyle(color: Colors.white), // ), // ), ); } } |
Output:
On running the application for the very first time you will find the initState method getting triggered i.e. it is called first and after that, the control enters the Build Context.
But, if you execute it again by invoking the class you will find the initState() method getting overridden and the next print statement will come to form the Build Context.