'LateError (LateInitializationError: Field 'documentSnapshot' has not been initialized)
I am using Cloud Firestore in my Flutter application and I am facing this issue in app: LateError (LateInitializationError: Field 'documentSnapshot' has not been initialized). I am using documentSnapshot in app but I don't know what should be used in its initialization as it takes a field value from Cloud Firestore.
I have declared a late documentSnapshot. and here I am reading the data from database using that documentSnapshot. It is throwing an error as it is a late variable and I am accessing it before initializing.
This is the code :
Future<int> getBudget(userKey) async {
var check = await FirebaseFirestore.instance
.collection(userKey!.email!)
.doc('limit')
.get();
if (check.exists) {
FirebaseFirestore.instance
.collection(userKey!.email!)
.doc('limit')
.get()
.then((value) {
documentSnapshot = value;
setState(() {
_budget = documentSnapshot['limit'];
});
});
return documentSnapshot['limit'];
}
return 0;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|