The build method is designed in such a way that it should be pure/without side effects. This is because many external factors can trigger a new widget build, such as:
- Route pop/push
- Screen resize, usually due to keyboard appearance or orientation change
- Parent widget recreated its child
- An InheritedWidget the widget depends on (
Class.of(context)
pattern) change
This means that the build
method should not trigger an http call or modify any state.
How is this related to the question?
The problem you are facing is that your build method has side-effects/is not pure, making extraneous build call troublesome.
Instead of preventing build call, you should make your build method pure, so that it can be called anytime without impact.
In the case of your example, you'd transform your widget into a StatefulWidget
then extract that HTTP call to the initState
of your State
:
class Example extends StatefulWidget {
@override
_ExampleState createState() => _ExampleState();
}
class _ExampleState extends State<Example> {
Future<int> future;
@override
void initState() {
future = Future.value(42);
super.initState();
}
@override
Widget build(BuildContext context) {
return FutureBuilder(
future: future,
builder: (context, snapshot) {
// create some layout here
},
);
}
}
I know this already. I came here because I really want to optimize rebuilds
It is also possible to make a widget capable of rebuilding without forcing its children to build too.
When the instance of a widget stays the same; Flutter purposefully won't rebuild children. It implies that you can cache parts of your widget tree to prevent unnecessary rebuilds.
The easiest way is to use dart const
constructors:
@override
Widget build(BuildContext context) {
return const DecoratedBox(
decoration: BoxDecoration(),
child: Text("Hello World"),
);
}
Thanks to that const
keyword, the instance of DecoratedBox
will stay the same even if build were called hundreds of times.
But you can achieve the same result manually:
@override
Widget build(BuildContext context) {
final subtree = MyWidget(
child: Text("Hello World")
);
return StreamBuilder<String>(
stream: stream,
initialData: "Foo",
builder: (context, snapshot) {
return Column(
children: <Widget>[
Text(snapshot.data),
subtree,
],
);
},
);
}
In this example when StreamBuilder is notified of new values, subtree
won't rebuild even if the StreamBuilder/Column do.
It happens because, thanks to the closure, the instance of MyWidget
didn't change.
This pattern is used a lot in animations. Typical uses are AnimatedBuilder
and all transitions such as AlignTransition
.
You could also store subtree
into a field of your class, although less recommended as it breaks the hot-reload feature.
Credit to @Remi, initState()
is a method which is called once when the stateful widget is inserted in the widget tree.
We generally override this method if we need to do some sort of initialisation work like registering a listener because, unlike build()
, this method is called once.
And to unregister your listener (or doing some post work), you override dispose()
method.
From here
A subclass of State can override initState to do work that needs to happen just once. For example, override initState to configure animations or to subscribe to platform services. Implementations of initState are required to start by calling super.initState
When a state object is no longer needed, the framework calls dispose() on the state object. Override the dispose function to do cleanup work. For example, override dispose to cancel timers or to unsubscribe from platform services. Implementations of dispose typically end by calling super.dispose
Best Answer
At the time build is called, context is available to us and is passed as an argument.
Now moving on, initstate is called before the state loads its dependencies and for that reason no context is available and you get an error for that if u use context in initstate. However didChangeDependencies is called just few moments after the state loads its dependencies and context is available at this moment so here you can use context.