How to use data from Provider during initState in Flutter application
Solution 1:
You need to use the context to call Provider.of()
so you can add addPostFrameCallback()
which is called after the first build, there you can use the context
@override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
auth = Provider.of<Auth>(context, listen: false);
});
}
Solution 2:
From Provider v4.1.0
, you can also use read() method. It reduces the boilerplate code.
@override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
auth = context.read<Auth>();
});
}