I'm getting green swirly lines underneath the await keyword.
When I hover the mouse over the green swirly lines, it says 'await' applied to 'Query', which is not a 'Future'
.
I'm not sure to which statement the await keyword needs to be applied.
Without applying the await/async and Future to the method the calling method doesn't wait for this method to get completed and continues with executing the rest of the code, therefore returning a null value.
Please find below the code which is in question:
Future<List<ContData>> readCC(String tID) async {
List<ContData> conList = [];
try {
final conCollection = await FirebaseFirestore.instance
.collection('CC')
.where('conID', isEqualTo: tID);
await conCollection.snapshots().listen((snapshot) {
var tc = snapshot.docs[0].data()['con'];
ContData con;
for (var t in tc) {
con = ContData.fromMap(data: t);
print(con);
final conType = con.conType;
final conText = con.conText;
final conCodeCaption = con.conCodeCaption;
final conCodeText = con.conCodeText;
final conCodeOutput = con.conCodeOutput;
final conImageFilename = con.conImageFilename;
final conImageCaption = con.conImageCaption;
final conImageCredit = con.conImageCredit;
final conImageCreditLink = con.conImageCreditLink;
final conAnchorText = con.conAnchorText;
final conAnchorLink = con.conAnchorLink;
final ContData = ContData(
conType: conType,
conText: conText,
conCodeCaption: conCodeCaption,
conCodeText: conCodeText,
conCodeOutput: conCodeOutput,
conImageFilename: conImageFilename,
conImageCaption: conImageCaption,
conImageCredit: conImageCredit,
conImageCreditLink: conImageCreditLink,
conAnchorText: conAnchorText,
conAnchorLink: conAnchorLink);
}
});
} catch (e) {
print(e.toString());
}
return conList;
}
Thank you so much for your precious time and help in advance!
You can only add await
to a method that returns a Future
, for example:
final conCollection = await FirebaseFirestore.instance
.collection('CC')
.where('conID', isEqualTo: tID).get();
The method get()
returns a Future<QuerySnapshot>
therefore instead of using then()
which takes a callback that will be called when the future is completed.
Read the following guide: