【发布时间】:2019-10-28 16:37:09
【问题描述】:
只是有点困惑我什么时候应该使用哪个?有什么区别?
async await 在完成之前不会执行函数中的下一行代码吗?它是否被拉出函数的一般顺序?如果是,then 做了什么,有什么不同?
如果我想确保在调用该方法以获取值之前已完成某些操作,以便它不返回 null,我应该使用哪个?
例如,我想从数据库中获取信息,然后在屏幕加载后立即为该数据设置一个变量,因此我在 initState() 中定义了它,
@override
void initState() {
// TODO: implement initState
super.initState();
currentUser= new User();
currentUser.getInfo().then((_) =>setState(() { bio = currentUser.getBio(); print(bio); }));
}
getInfo 是一个async 函数,我尝试了这个,但最终发生的事情是它首先以某种方式打印 null,然后再打印从 getinfo 方法内部调用的实际 bio。如何切换顺序?
更新: 这是用户类:
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
class User {
final _firestore = Firestore.instance;
final _auth= FirebaseAuth.instance;
FirebaseUser loggedInUser;
String displayName;
String email;
String bio;
String photoUrl;
Future<void> getCurrentUser() async{
try{
final user= await _auth.currentUser();
if(user!=null){
loggedInUser=user;
email=loggedInUser.email;
}}
catch(e){
print(e);
}
}
Future<void> getInfo() async {
await getCurrentUser();
DocumentReference documentReference =
_firestore.collection("users").document("$email");
documentReference.get().then((DocumentSnapshot datasnapshot) {
if (datasnapshot.exists) {
displayName=datasnapshot.data['displayName'].toString();
bio=datasnapshot.data['bio'].toString();
print(bio);
}
else {
print("No such user");
}
});
}
User({String bio,String displayName}){
if(bio!=null){
this.bio= bio;
print(this.bio);
}
if(displayName!=null){
this.displayName = displayName;
}
}
void updateData({String bio, String displayName}){
if(bio!=null){
this.bio=bio;
print(this.bio);
}
if(displayName!=null){
this.displayName=displayName;
}
_firestore.collection('users').document('$email').setData({
'bio':this.bio,
'displayName':this.displayName
});
}
String getBio(){
return bio;
}
}
更新:
将 getinfo 更改为这个,现在它可以工作了,但真的不明白为什么:
Future<void> getInfo() async {
await getCurrentUser();
DocumentReference documentReference =
_firestore.collection("users").document("$email");
await documentReference.get().then((DocumentSnapshot datasnapshot) {
if (datasnapshot.exists) {
displayName=datasnapshot.data['displayName'].toString();
bio=datasnapshot.data['bio'].toString();
print(bio);
}
else {
print("No such user");
}
});
}
【问题讨论】:
-
张贴
User.getBio()代码(甚至更好的是整个User类) -
User.getBio() 实际上只是一个 getter 方法,但我知道我获取数据的方式是有效的,因为 getInfo 中的 print 语句会打印出 bio 的实际值,但仅在第一次打印之后initstate 中的语句被打印为 null
-
为什么在
getInfo方法中混合await和then?保持一致并仅使用await -
我不确定,我使用 await 等待 getCurrentUser 完成,以便我可以使用当前用户的电子邮件。我怎样才能重新格式化它只使用'then'?同样,我不确定我是否理解其中的差异,但改变它会解决问题吗?
标签: asynchronous flutter dart