App LifeCycle In Flutter.

Sanket Masurkar
Nerd For Tech
Published in
3 min readJan 12, 2023

Hello Everyone!👋

If you’re here, it implies that you might be curious about what happens behind the scenes when your app runs.

The Flutter app lifecycle refers to the several stages that an app goes through as it starts up, runs, and shuts down. Understanding the app lifecycle is crucial because it enables you to build code that executes at specified moments in the lifecycle, such as when the app is being initialized or when it is being closed down.

There are several app lifecycle hooks available in Flutter. These hooks allow the app to execute code at specific points in the app’s lifecycle, such as when the app is being initialized, when the app is becoming visible or inactive, and when the app is being closed. The lifecycle hooks are listed as follows:

1.initState()
2.didChangeDependencies()
3.build()
4.didUpdateWidget()
5.dispose()

Here’s a brief overview of each hook:

initState()

The initState() method is a lifecycle hook that is called when the app is being initialized. This method is a good place to initialize any state for the app, such as loading data from a remote server or connecting to a database.

Here’s an example of how the initState() method may be used in a Flutter app:

class InitExample extends StatefulWidget {
@override
_InitExampleState createState() => _InitExampleState();
}

class _InitExampleState extends State<InitExample> {
String data;

@override
void initState() {
super.initState();
fetchData().then((value) {
setState(() {
data = value;
});
});
}

@override
Widget build(BuildContext context) {
return Scaffold(
body: data != null ? Text(data) : CircularProgressIndicator(),
);
}
}

The initState() method is used in the preceding example to retrieve data from a server and save it in the data variable. The data is then shown on the screen using the build() method. A loading sign is displayed instead if the data is not yet available.

didChangeDependencies()

Every time a widget’s dependencies change, the didChangeDependencies() method is called. Understanding this method is important as it helps you to update a widget’s state depending on changes to its dependencies.

Here’s how you might use the didChangeDependencies() method in a Flutter app:

 @override
void didChangeDependencies() {
super.didChangeDependencies();

}

It’s important to note that the didChangeDependencies() hook is called whenever the dependencies of a widget change, not just when the inherited widget's value changes. This means that you should be careful to only update the state of the widget when necessary, as updating the state unnecessarily can lead to performance issues.

build()

The build() method is used to create the user interface for your app.
This method should return a widget that represents the app's user interface. It’s called every time the app needs to update the way it looks, and it’s responsible for creating the widgets that make up the app’s user interface.

Here’s a simple example of how the build() method is used:

class InitExample extends StatefulWidget {
@override
_InitExampleState createState() => _InitExampleState();
}

class _InitExampleState extends State<InitExample> {
String data = "This is an example";

@override
Widget build(BuildContext context) {
return Scaffold(
body: Text(data),
);
}
}

The build() method is called frequently, so it's important to make sure that it's efficient and only does what's necessary to create the UI. This helps to keep the app working smoothly and swiftly.

didUpdateWidget()

The didUpdateWidget() method is called every-time the widget is rebuilt and its dependencies have changed. This method is useful for updating the state of a widget based on changes to its dependencies.

Here’s how you might use the didUpdateWidget() method in a Flutter app:

@override
void didUpdateWidget(MyApp oldWidget) {
super.didUpdateWidget(oldWidget);
}

The didUpdateWidget() method is called whenever the widget is rebuilt and its dependencies have changed, so it's important to make sure that it's efficient and only does what's necessary to update the widget's state.

dispose()

The dispose() method is used for cleaning up any resources that the state is holding onto when it is closed.


StreamSubscription _streamSubscription;

@override
void dispose() {
super.dispose();
_streamSubscription.cancel();
}

In the preceding example, the dispose() method is used to close the StreamSubscription connection. This frees up resources and ensures that the app is running efficiently.

Conclusion:

To develop a smooth and seamless user experience in your Flutter app, you must first understand the app lifecycle.

I hope you found this quick explanation of the Flutter app lifecycle useful! If you have any questions, don’t hesitate to reach out.

Clap 👏 If you like the article, please share it.

--

--

Sanket Masurkar
Nerd For Tech

Flutter enthusiast by day, dart-throwing champion by night.