You are on page 1of 9

Gand mara le

Chutiya
Madar chod
Fuddu
Gand mara le
Chutiya
Madar chod
Fuddu

Gand mara le
Chutiya
Madar chod
Fuddu

Gand mara le
Chutiya
Madar chod
Fuddu
Gand mara le
Chutiya
Madar chod
Fuddu

Gand mara le
Chutiya
Madar chod
Fuddu

Gand mara le
Chutiya
Madar chod
Fuddu

Gand mara le
Chutiya
Madar chod
Fuddu

Gand mara le
Chutiya
Madar chod
Fuddu
Gand mara le
Chutiya
Madar chod
Fuddu
Stateful Widget lifecycle

1. createState(): When the Framework is instructed to build a StatefulWidget,


it immediately calls createState()
2. mounted is true: When createState creates your state class, a buildContext
is assigned to that state. BuildContext is, overly simplified, the place in the widget
tree in which this widget is placed. Here's a longer explanation. All widgets have
a bool this.mounted property. It is turned true when the buildContext is
assigned. It is an error to call setState when a widget is unmounted.
3. initState(): This is the first method called when the widget is created (after
the class constructor, of course.) initState is called once and only once. It must
called super.initState().
4. didChangeDependencies(): This method is called immediately after
initState on the first time the widget is built.
5. build(): This method is called often. It is required, and it must return a
Widget.
6. didUpdateWidget(Widget oldWidget): If the parent widget changes and
has to rebuild this widget (because it needs to give it different data), but it's being
rebuilt with the same runtimeType, then this method is called. This is because
Flutter is re-using the state, which is long lived. In this case, you may want to
initialize some data again, as you would in initState.
7. setState(): This method is called often from the framework itself and from
the developer. Its used to notify the framework that data has changed
8. deactivate(): Deactivate is called when State is removed from the tree, but it
might be reinserted before the current frame change is finished. This method
exists basically because State objects can be moved from one point in a tree to
another.
9. dispose(): Dispose is called when the State object is removed, which is
permanent. This method is where you should unsubscribe and cancel all
animations, streams, etc.
10. mounted is false: The state object can never remount, and an error is
thrown is setState is called.

Example

import 'package:flutter/material.dart';

class ScreenLifecyle extends StatefulWidget {


ScreenLifecyleState state;

//createState(): When the Framework is instructed to build a


StatefulWidget, it immediately calls createState()
@override
State<StatefulWidget> createState() {
// TODO: implement createState
return ScreenLifecyleState();
}
}

class ScreenLifecyleState extends State<ScreenLifecyle> {


/*
mounted is true: When createState creates your state class, a
buildContext is assigned to that state.
BuildContext is, overly simplified, the place in the widget tree in
which this widget is placed.
Here's a longer explanation. All widgets have a bool this.mounted
property.
It is turned true when the buildContext is assigned. It is an error
to call setState when a widget is unmounted.
mounted is false: The state object can never remount, and an error
is thrown is setState is called.
*/

/*
This is the first method called when the widget is created (after
the class constructor, of course.)
initState is called once and only once. It must called
super.initState().
*/
@override
void initState() {
// TODO: implement initState
super.initState();
print("initState");
}

/*
This method is called immediately after initState on the first time
the widget is built.
*/
@override
void didChangeDependencies() {
// TODO: implement didChangeDependencies
super.didChangeDependencies();
print("didChangeDependencies");
}

/*
build(): This method is called often. It is required, and it must
return a Widget.
*/
@override
Widget build(BuildContext context) {
print("build");

// TODO: implement build


return Container();
}

/*
If the parent widget changes and has to rebuild this widget (because
it needs to give it different data),
but it's being rebuilt with the same runtimeType, then this method
is called.
This is because Flutter is re-using the state, which is long lived.
In this case, you may want to initialize some data again, as you
would in initState.
*/
@override
void didUpdateWidget(ScreenLifecyle oldWidget) {
print("didUpdateWidget");

// TODO: implement didUpdateWidget


super.didUpdateWidget(oldWidget);
}

@override
void setState(fn) {
print("setState");

// TODO: implement setState


super.setState(fn);
}

/*
Deactivate is called when State is removed from the tree,
but it might be reinserted before the current frame change is
finished.
This method exists basically because State objects can be moved from
one point in a tree to another.
*/
@override
void deactivate() {
// TODO: implement deactivate
print("deactivate");
super.deactivate();
}

/*
Dispose is called when the State object is removed, which is
permanent.
This method is where you should unsubscribe and cancel all
animations, streams, etc.
*/
@override
void dispose() {
// TODO: implement dispose
super.dispose();
}

@override
void didChangeAppLifecycleState(AppLifecycleState state) {
super.didChangeAppLifecycleState(state);
switch (state) {
case AppLifecycleState.inactive:
print('appLifeCycleState inactive');
break;
case AppLifecycleState.resumed:
print('appLifeCycleState resumed');
break;
case AppLifecycleState.paused:
print('appLifeCycleState paused');
break;
case AppLifecycleState.suspending:
print('appLifeCycleState suspending');
break;
}
}

Example 2

Something that's been brought up recently was the question about how to stop services when
the app goes into the background. This post will show you how to handle all that in one place
in an extendable way. We'll be using the WidgetsBindingObserver to listen to
the AppLifecycleState and call stop/start on our services. We'll start by creating the Manager
and wrapping our app with it. Create a new file called lifecycle_manager.dart
class LifeCycleManager extends StatefulWidget {
final Widget child;
LifeCycleManager({Key key, this.child}) : super(key: key);

_LifeCycleManagerState createState() => _LifeCycleManagerState();


}

class _LifeCycleManagerState extends State<LifeCycleManager>


with WidgetsBindingObserver {
@override
void initState() {
WidgetsBinding.instance.addObserver(this);
super.initState();
}

@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}

@override
void didChangeAppLifecycleState(AppLifecycleState state) {
print('state = $state');
}

@override
Widget build(BuildContext context) {
return Container(
child: widget.child,
);
}
}
Then in the main file we can wrap our MaterialApp with the new widget.
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return LifeCycleManager(
child: MaterialApp(
title: 'Flutter Demo',
home: HomeView(),
),
);
}
}
If you run this and you minimise your app you'll see the state logs being printed out in the
console.
state = AppLifecycleState.inactive
state = AppLifecycleState.paused
state = AppLifecycleState.inactive
state = AppLifecycleState.resumed
We'll use this to make our decision on what to call on our services. To give the services that's
subscribed to streams a common interface we'll create a StoppableService abstract class that
these services has to implement. We'll add some functionality to the base class to track if it's
been stopped so we can use that to our advantage as well.
abstract class StoppableService {
bool _serviceStoped = false;
bool get serviceStopped => _serviceStoped;

@mustCallSuper
void stop() {
_serviceStoped = true;
}

@mustCallSuper
void start() {
_serviceStoped = false;
}
}
And an implementation of this will look like this.
class LocationService extends StoppableService {
@override
void start() {
super.start();
// start subscription again
}

@override
void stop() {
super.stop();
// cancel stream subscription
}
}
We lack the benefits of reflection in Flutter so we'll have to keep track of these services in our
LifeCycleManager and loop over them to stop and start them. In the LifeCycleManager we'll
keep a list of the StoppableServices and loop over them in the lifecycle function call.
class _LifeCycleManagerState extends State<LifeCycleManager>
with WidgetsBindingObserver {

List<StoppableService> services = [
locator<LocationService>(),
];

...

@override
void didChangeAppLifecycleState(AppLifecycleState state) {
services.forEach((service) {
if (state == AppLifecycleState.resumed) {
service.start();
} else {
service.stop();
}
});
}
}

You might also like