I'm trying to call async functions in my initstate and I succeed, the problem is that unlike what it normally does build is executed before initstate. This is my code and of course it gives me an error because the late variables are not assigned before build:
late int oraNotifiche;
late int minutiNotifiche;
aggiornaImpostazioni() async {
final prefs = await SharedPreferences.getInstance();
await checkNotificheCalendario();
int timestap = await prefs.getInt("oraNotifiche") ??
DateTime(DateTime.now().year, DateTime.now().month,
DateTime.now().day - 1, 19, 0)
.millisecondsSinceEpoch;
DateTime orarioSalvato = DateTime.fromMillisecondsSinceEpoch(timestap);
oraNotifiche = orarioSalvato.hour;
minutiNotifiche = orarioSalvato.minute;
if (!mounted) return;
setState(() {});
}
@override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((_) async {
await aggiornaImpostazioni();
});
}