Skip to content

Instantly share code, notes, and snippets.

@aduquet
Created June 21, 2022 19:32
Show Gist options
  • Save aduquet/f04b5f780f9d34c6e031ce0b74af5ab7 to your computer and use it in GitHub Desktop.
Save aduquet/f04b5f780f9d34c6e031ce0b74af5ab7 to your computer and use it in GitHub Desktop.
Async data on InitState method - dart/flutter
I was struggling in finding a way to load async data on Initial State method
I found the answer here: https://stackoverflow.com/questions/51901002/is-there-a-way-to-load-async-data-on-initstate-method
This is my concrete example
setInitialValueQuestionReview() async {
late DocumentReference<Map<String, dynamic>> testAnwDB = FirebaseFirestore
.instance
.collection('AppUsers')
.doc(currentUser!.uid)
.collection('test_settings')
.doc('personalised_Settings');
var querySnapshot = await testAnwDB.get();
var personalisedSettingsData = querySnapshot.data();
_questionReview.text = personalisedSettingsData!['reviewSesion'].toString();
_isSelectedShowBoth = personalisedSettingsData['showBoth'];
_isSelectedShowCorrectas = personalisedSettingsData['showCorr'];
_isSelectedShowIncorrectas = personalisedSettingsData['showInCorr'];
}
@override
void initState() {
super.initState();
WidgetsBinding.instance?.addPostFrameCallback((_) {
setInitialValueQuestionReview();
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment