【发布时间】:2020-07-09 13:38:54
【问题描述】:
我正在做一个论坛移动应用程序,我想在用户上传帖子时显示用户的个人资料图片和用户名。我已将一个 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 flutter dart google-cloud-firestore