InitState() - Flutter

 We can create a cross-platform application on Flutter the same as a native application. Flutter was developed by Facebook. We can create two types of coding in the Flutter. The first one is a Stateless widget and the other is a stateful widget of Mobile application.

  1. The Stateless widget 
  2. The stateful widget

In the Flutter application, we can use stateful widgets that time some states call while executing the application. The initState() is a method that is called when an object for a stateful widget is created. It is the entry point of a stateful widget. The InitState() method is used to initialize the variables. 

Syntex 

 @override
initState() {
print("initState Called");
}

Let's take example of initState()  method.


Step-by-Step Implementation

Step 1: Create a New Project in Android Studio (File >new flutter project)


Step 2: Adding material package.
Import method the runApp method in the main function call first while run the application.

 import 'package:flutter/material.dart';

 void main() {

     runApp(RunMyApp());

 }

Step 3: Write the initstate() method of main.dart file
 import 'package:flutter/material.dart';

void main() {
runApp(HomeScreen());
}

class HomeScreen extends StatelessWidget {
const HomeScreen({Key? key}) : super(key: key);

@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text(
"CandidRoot Solutions",
),
backgroundColor: Colors.blue,
),
body: const stateExample(),
),
);
}
}
class stateExample extends StatefulWidget {
const stateExample({Key? key}) : super(key: key);
@override
State<stateExample> createState() => _stateExampleState();
}

class _stateExampleState extends State<stateExample> {
@override
initState() {
print("initState Called");
}

@override
Widget build(BuildContext context) {
print(" Build method called");
return Center(
child: ElevatedButton(
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(Colors.blue)),
onPressed: () {},
child: const Text(
'initState Demonstration',
style: TextStyle(color: Colors.white),
),
),
);
}
}

 

Step 4: Final code
 import 'package:flutter/material.dart';

void main() {
runApp(HomeScreen());
}

class HomeScreen extends StatelessWidget {
const HomeScreen({Key? key}) : super(key: key);

@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text(
"CandidRoot Solutions",
),
backgroundColor: Colors.blue,
),
body: const stateExample(),
),
);
}
}
class stateExample extends StatefulWidget {
const stateExample({Key? key}) : super(key: key);
@override
State<stateExample> createState() => _stateExampleState();
}

class _stateExampleState extends State<stateExample> {
@override
initState() {
print("initState Called");
}

@override
Widget build(BuildContext context) {
print(" Build method called");
return Center(
child: ElevatedButton(
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(Colors.blue)),
onPressed: () {},
child: const Text(
'initState Demonstration',
style: TextStyle(color: Colors.white),
),
),
);
}
}

 

Step 5: Output of above example 

Happy coding!

365Bloggy June 6, 2024
Share this post
Tags
SUBSCRIBE THIS FORM


Archive