我正在做一个论坛移动应用程序,我想在用户上传帖子时显示其个人资料图片和用户名。我已经将一个ownerid字段(这是发布人员的uid)以及发布详细信息存储到firebase中。使用uid,如何访问用户的显示名称和个人资料图片?
这是我的帖子课:
with responses as (
select id, client_id, user_id,
row_number()
over (order by client_id, user_id, id)
as dialog_id
from conversations
where incoming = false
), dialogs as (
select c.*, min(dialog_id) as dialog_id
from conversations c
join responses r
on r.client_id = c.client_id
and r.user_id = c.user_id
and r.id >= c.id
group by c.id, c.client_id, c.message, c.user_id, c.incoming
)
select dialog_id, client_id,
array_agg(message order by id)
filter (where incoming = true)
as client_message,
user_id,
max(message)
filter (where incoming = false)
as user_message
from dialogs
group by dialog_id, client_id, user_id
order by dialog_id;
//在此使用了未来构建器
class Post { //for forums
String title;
String content;
String timestamp;
String imageurl;
String username;
String profilePicture;
String documentid;
String ownerid;
Map<String, dynamic> saved = {};
Map<String, dynamic> upvotes = {};
Post(
this.title,
this.content,
this.timestamp,
this.imageurl,
this.username,
this.profilePicture,
this.documentid,
this.ownerid,
this.saved,
this.upvotes
//this.image
);
Post.fromSnapshot(DocumentSnapshot snapshot) :
title = snapshot["title"],
content = snapshot['content'],
timestamp = snapshot['timestamp'],
imageurl = snapshot['imageurl'],
username = snapshot['username'],
profilePicture = snapshot['profilePicture'],
documentid = snapshot['documentid'],
upvotes = snapshot['upvotes'],
ownerid = snapshot['ownerid'],
saved = snapshot['saved'];
}
FutureBuilder(
future: Firestore.instance.collection('users').document(post['ownerid']).get(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
return Text(snapshot.data['username'], style: TextStyle(fontWeight: FontWeight.bold,
fontSize: 16, decoration: TextDecoration.underline, color: Colors.grey[100]),
);
} else {
return CircularProgressIndicator();
}
},
),
答案 0 :(得分:1)
假设您在Firebase中已经有一个名为“用户”的集合,您将在其中存储登录时的用户数据。还有一个名为“ uid”的字段,您将在其中存储uid
在firebase中检查具有相同uid的用户并提取详细信息
QuerySnapshot snapshot = awaitFirestore.instance.collection("users").where("uid",isEqualTo:ownerId).getDocuments()
//if you are sure that there is exactly one user with the same uid
Map<String,dynamic> userInfo = snapshot.documents[0].data;
要保存读取,我建议使用用户的uid来命名“用户”集合的文档ID,因为在这种情况下,您可以进行直接查询,例如
DocumentSnapshot doc = await Firestore.instance.collection("users").document(ownerId).get();
//To access any fields on the document retrieved
String username = doc.data["username"] //assuming the fields name in the document
//is "username"
答案 1 :(得分:0)
除控制台外,您无权直接访问所有经过Firebase身份验证的用户。
一种解决方法是拥有一个users
集合,您可以在注册后存储所有需要的用户信息。由于您拥有帖子中的用户ID,因此将其用作users
集合中每个用户的文档ID会更容易。然后,您可以使用帖子中的用户ID从users
集合中获取他的文档,其中将包含他的所有详细信息。