gpt4 book ai didi

firebase - 异步功能: future <查询快照> flutter flutter ?

转载 作者:行者123 更新时间:2023-12-03 04:44:14 25 4
gpt4 key购买 nike

我在这里有这段代码:如果用户存在于数据库中,并且紧随其后的是CurrentUser,则会构建一个“取消关注”自定义按钮,否则它将构建一个关注按钮

CustomButton follow = CustomButton("Follow", Colors.red, Colors.red, Colors.white, user);
CustomButton unfollow = CustomButton("Unfollow", Colors.black, Colors.black, Colors.white, user);

AvatarHeader(user.username, " ", user.photoURL, checkIfFollowing(user.id)== true ? unfollow : follow)
checkIfFollowing()函数始终被评估为false,可能是因为我在此asyncronus函数中遇到问题,该函数未返回 bool(boolean) 值
  checkIfFollowing(String fetchedUser) async{
DocumentSnapshot doc = await FOLLOWERS
.document(fetchedUser)
.collection('userFollowers')
.document(CURRENTUSER.id)
.get()

return doc.exists;
}
我怎样才能解决这个问题?
编辑
  search(String query) {
Future<QuerySnapshot> users = USERS
.where("username", isGreaterThanOrEqualTo: query)
.getDocuments();

print(query);
print("");
onStringChange(users);

}


FutureBuilder buildResults(){
return FutureBuilder (
future: results,
builder: (context, snapshot) {
if (!snapshot.hasData) {
print("i dont have data");
return circularProgress();
}
List<AvatarHeader> searchResults = [];
snapshot.data.documents.forEach((doc) async {
User user = User.fromDocument(doc);
if (user.photoURL != null) {
print(user.username);
bool check = await checkIfFollowing(user.id);
CustomButton follow = CustomButton("Follow", Colors.red, Colors.red, Colors.white, user);
CustomButton unfollow = CustomButton("Unfollow", Colors.black, Colors.black, Colors.white, user);
searchResults.add(AvatarHeader(user.username, " ", user.photoURL, check == true ? unfollow : follow));
}
});
return ListView(
shrinkWrap: true,
physics: ClampingScrollPhysics(),
children: searchResults,
);
},
);
}

最佳答案

您需要在异步函数中返回Future,在特定情况下,您需要Future<bool>

Future<bool> checkIfFollowing(String fetchedUser) async{
DocumentSnapshot doc = await FOLLOWERS
.document(fetchedUser)
.collection('userFollowers')
.document(CURRENTUSER.id)
.get()

return doc.exists;
}
我建议您看看 Asynchronous programming: futures, async, await

A future (lower case “f”) is an instance of the Future (capitalized “F”) class. A future represents the result of an asynchronous operation, and can have two states: uncompleted or completed.


编辑
没有更多细节,很难确定如何解决您的特定问题。如果我理解正确,则需要与此保持一致
FutureBuilder buildResults() {
return FutureBuilder(
future: results,
builder: (context, snapshot) {
if (!snapshot.hasData) {
print("i dont have data");
return circularProgress();
}

// get documents where user.photoURL != null
var docsWhereUserPhotoIsNotNull = getDocumentsWithUserPhotoNotNull(snapshot.data.documents);

// build a list of FutureBuilders
return ListView.builder(
shrinkWrap: true,
physics: ClampingScrollPhysics(),
itemCount: docsWhereUserPhotoIsNotNull.length,
itemBuilder: (context, index) {
var doc = docsWhereUserPhotoIsNotNull[index];
var user = User.fromDocument(doc);

return FutureBuilder(
future: checkIfFollowing(user.id),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return circularProgress();
}

bool check = snapshot.data;
CustomButton follow = CustomButton("Follow", Colors.red, Colors.red, Colors.white, user);
CustomButton unfollow = CustomButton(
"Unfollow", Colors.black, Colors.black, Colors.white, user);
return AvatarHeader(user.username, " ", user.photoURL, check == true ? unfollow : follow);
});
});
}
);
}
哪里
List<DocumentSnapshot> getDocumentsWithUserPhotoNotNull(List<DocumentSnapshot> documents) {
var documentsWithUserPhotoNotNull = List<DocumentSnapshot>();

documents.forEach((doc) async {
User user = User.fromDocument(doc);
if (user.photoURL != null) {
documentsWithUserPhotoNotNull.add(doc);
}
});

return documentsWithUserPhotoNotNull;
}

关于firebase - 异步功能: future <查询快照> flutter flutter ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62582103/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com