我正在做一个论坛移动应用程序,我想在用户上传帖子时显示其个人资料图片和用户名。我已经将一个ownerid字段(这是发布人员的uid)以及发布详细信息存储到firebase中。使用uid,我如何访问用户的显示名和个人资料图片?
这是我的帖子课:
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();
}
},
),
//在这里使用 future builder Widget buildForum(BuildContext context, DocumentSnapshot post) {
final forum = Post.fromSnapshot(post);
return Container(
child: Card(
color: Colors.grey[850],
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(18.0)),
child: InkWell(
onTap: () {
Navigator.push(context, MaterialPageRoute(
builder: (context) => ForumDetails(forum: forum) //with this particular forum
));
},
child: Padding(
padding: EdgeInsets.only(top: 4, bottom: 4),
child: Column(
children: <Widget>[
Padding(
padding: EdgeInsets.only(top: 4, bottom: 8),
child:
Row(children: <Widget>[
SizedBox(width: 10,),
Text('Uploaded on ', style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold,
color: Colors.grey[400]),),
Text(post['timestamp'],
style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold,
color: Colors.grey[400]),),
Spacer(),
],),
),
SizedBox(height: 10),
Row(children: <Widget>[
SizedBox(width: 10,),
CircleAvatar(
backgroundImage: post['profilePicture'] != null ?
NetworkImage(post['profilePicture']) :
NetworkImage('https://genslerzudansdentistry.com/wp-content/uploads/2015/11/anonymous-user.png'),
backgroundColor: Colors.grey,
radius: 20,),
SizedBox(width: 10,),
//post['ownerid']
// Text(post['username'], style: TextStyle(fontWeight: FontWeight.bold,
// fontSize: 16, decoration: TextDecoration.underline, color: Colors.grey[100]),
// ),
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();
}
},
),
],),
SizedBox(height: 10),
Padding(
padding: EdgeInsets.only(top: 4, bottom: 8),
child:
Row(children: <Widget>[
SizedBox(width: 10,),
Expanded(child:
Text(post['title'], style: TextStyle(fontSize: 18,
fontWeight:FontWeight.bold, color: Colors.grey[100]))),
],)),
//display image if there is
(post['imageurl'] != null) ?
Padding(
padding: EdgeInsets.only(top: 4, bottom: 8),
child:
Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
SizedBox(width: 10,),
ClipRRect(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(8.0),
topRight: Radius.circular(8.0),
),
child: Image.network(post['imageurl']),
),
//image of notes
],),) : Container(height:0),
Padding(
padding: EdgeInsets.only(top: 4, bottom: 8),
child:
Row(children: <Widget>[
SizedBox(width: 10,),
Expanded(child:
Text(post['content'], style: TextStyle(fontSize: 16, color: Colors.grey[100]),
overflow: TextOverflow.ellipsis, maxLines: 2,),),
],)),
SizedBox(height: 20),
Row(children: <Widget>[
SizedBox(width: 10,),
Icon(Icons.comment, size: 26,
color: Colors.tealAccent),
SizedBox(width: 6,),
Text('0', style: TextStyle(color: Colors.grey[100]),), //change to icons
Spacer(),
Icon(Icons.thumb_up, size: 26, color: Colors.tealAccent),
SizedBox(width: 6,),Text(post['upvotes'].values.where((e)=> e as bool).length.toString(), style: TextStyle(color: Colors.grey[100]),),
SizedBox(width: 10,)],)
],)
),),)
);
}
最佳答案
假设您在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"