不确定在 Flutter 或 Dart 中 await 关键字需要应用于哪个语句
Not sure to which statement the await keyword needs to be applied in Flutter or Dart
我在 await 关键字下方看到绿色的漩涡线。
当我将鼠标悬停在绿色漩涡线上时,它显示 'await' applied to 'Query', which is not a 'Future'
。
我不确定 await 关键字需要应用于哪个语句。
如果不将 await/async 和 Future 应用于方法,调用方法不会等待此方法完成并继续执行其余代码,因此返回空值。
请在下面找到有问题的代码:
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;
}
非常感谢您的宝贵时间和提前帮助!
您只能将await
添加到returns一个Future
的方法中,例如:
final conCollection = await FirebaseFirestore.instance
.collection('CC')
.where('conID', isEqualTo: tID).get();
方法 get()
returns a Future<QuerySnapshot>
因此,而不是使用 then()
,它接受一个回调,它将在未来完成时被调用。
阅读以下指南:
我在 await 关键字下方看到绿色的漩涡线。
当我将鼠标悬停在绿色漩涡线上时,它显示 'await' applied to 'Query', which is not a 'Future'
。
我不确定 await 关键字需要应用于哪个语句。
如果不将 await/async 和 Future 应用于方法,调用方法不会等待此方法完成并继续执行其余代码,因此返回空值。
请在下面找到有问题的代码:
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;
}
非常感谢您的宝贵时间和提前帮助!
您只能将await
添加到returns一个Future
的方法中,例如:
final conCollection = await FirebaseFirestore.instance
.collection('CC')
.where('conID', isEqualTo: tID).get();
方法 get()
returns a Future<QuerySnapshot>
因此,而不是使用 then()
,它接受一个回调,它将在未来完成时被调用。
阅读以下指南: