Flutter,我如何检索用户信息并将其显示在 firestore 的特定博客文章中?
Flutter, how can I retrieve user info and display it on specific blog posts from firestore?
我是来自 java(android) 的 dart 和 flutter 新手。我有一个 post 的列表,由不同的用户 post 编辑。我想为每个 post 显示用户名和用户图像。问题是我将用户存储在 "Users" 集合中,将 post 存储在 "Posts" 集合中。我在每个 post 上附加了用户 ID,我可以将它(id)与 post 详细信息一起提取。但我不能用它(id)来显示个人资料信息。请帮助,一些代码行可以给我一些启发,将不胜感激。
下面是我显示 posts:
的代码
body: StreamBuilder(
stream: Firestore.instance.collection('Posts').snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
const Text('Loading...');
} else {
return ListView.builder(
itemCount: snapshot.data.documents.length,
itemBuilder: (context, index) {
DocumentSnapshot myPosts = snapshot.data.documents[index];
return Container(
width: MediaQuery.of(context).size.width,
//height: 350,
child: Padding(
padding: EdgeInsets.only(bottom: 8.0),
child: Center(
child: Padding(
padding: EdgeInsets.all(8.0),
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Row(
children: <Widget>[
CircleAvatar(
radius: 20.0,
backgroundColor: Colors.blueGrey,
),
Padding(
padding: const EdgeInsets.only(left: 8.0),
child: Container(
color: Colors.black45,
height: 30,
width: 2,
),
),
Padding(
padding: const EdgeInsets.only(left: 8.0),
child: Column(
crossAxisAlignment:
CrossAxisAlignment.start,
children: <Widget>[
Text(
'user name',
style: TextStyle(
fontSize: 16.0,
fontWeight: FontWeight.bold,
color: Colors.black54,
),
overflow: TextOverflow.ellipsis,
),
Text(
'user status',
style: TextStyle(
color: Colors.grey,
fontSize: 14.0,
),
)
],
),
)
],
),
SizedBox(
height: 10.0,
),
Container(
width: MediaQuery.of(context).size.width,
//height: 200.0,
child: ClipRRect(
borderRadius: BorderRadius.circular(8.0),
child: AspectRatio(
aspectRatio: 16 / 9,
child: FadeInImage.assetNetwork(
width: MediaQuery.of(context).size.width,
placeholder: 'assets/images/loading.jpg',
image: '${myPosts['post_image']}',
fit: BoxFit.fill,
),
),
),
),
SizedBox(
height: 10.0,
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Flexible(
child: Padding(
padding: const EdgeInsets.only(right: 8.0),
child: Text(
'${myPosts['post_title']}',
style: TextStyle(
fontSize: 18.0,
fontWeight: FontWeight.bold,
color: Colors.black87,
),
maxLines: 2,
overflow: TextOverflow.ellipsis,
),
),
),
FlatButton(
child: Text(
'Download Pdf',
style: TextStyle(
fontSize: 14.0,
color: Colors.black54,
),
),
shape: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.black54, width: 1),
borderRadius: BorderRadius.circular(5),
),
padding: const EdgeInsets.fromLTRB(
15.0,
4.0,
15.0,
4.0,
),
onPressed: () {},
),
],
),
SizedBox(
height: 10.0,
),
Text(
'${myPosts['post_body']}',
style: TextStyle(
fontSize: 16.0,
),
overflow: TextOverflow.ellipsis,
maxLines: 8,
),
SizedBox(
height: 10.0,
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text(
'Posted ' +
TimeAgo.getTimeAgo(
myPosts['post_time']),
style: TextStyle(color: Colors.grey)),
Text(
'${myPosts['comments_count']} Comment(s)',
style: TextStyle(color: Colors.grey)),
],
),
SizedBox(
height: 10.0,
),
],
),
),
),
),
);
},
);
}
return Loading();
},
),
一切正常,问题在于提取用户信息。我有一个 class 用于在个人资料页面上显示用户数据,如果它在这种情况下可能有用,下面是它的代码
import 'package:cloud_firestore/cloud_firestore.dart';
class ProfileService {
getProfileInfo(String uid) {
return Firestore.instance
.collection('Users')
.where('user_id', isEqualTo: uid)
.getDocuments();
}
}
我被卡住了,非常感谢你的帮助。
尝试使用这个:
Future<DocumentSnapshot> _getDocument(String uid) async {
return await Firestore().collection('Users').where('user_id', isEqualTo: uid).get();
}
它将return记录快照,然后您可以使用它来获取用户信息
我是来自 java(android) 的 dart 和 flutter 新手。我有一个 post 的列表,由不同的用户 post 编辑。我想为每个 post 显示用户名和用户图像。问题是我将用户存储在 "Users" 集合中,将 post 存储在 "Posts" 集合中。我在每个 post 上附加了用户 ID,我可以将它(id)与 post 详细信息一起提取。但我不能用它(id)来显示个人资料信息。请帮助,一些代码行可以给我一些启发,将不胜感激。 下面是我显示 posts:
的代码body: StreamBuilder(
stream: Firestore.instance.collection('Posts').snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
const Text('Loading...');
} else {
return ListView.builder(
itemCount: snapshot.data.documents.length,
itemBuilder: (context, index) {
DocumentSnapshot myPosts = snapshot.data.documents[index];
return Container(
width: MediaQuery.of(context).size.width,
//height: 350,
child: Padding(
padding: EdgeInsets.only(bottom: 8.0),
child: Center(
child: Padding(
padding: EdgeInsets.all(8.0),
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Row(
children: <Widget>[
CircleAvatar(
radius: 20.0,
backgroundColor: Colors.blueGrey,
),
Padding(
padding: const EdgeInsets.only(left: 8.0),
child: Container(
color: Colors.black45,
height: 30,
width: 2,
),
),
Padding(
padding: const EdgeInsets.only(left: 8.0),
child: Column(
crossAxisAlignment:
CrossAxisAlignment.start,
children: <Widget>[
Text(
'user name',
style: TextStyle(
fontSize: 16.0,
fontWeight: FontWeight.bold,
color: Colors.black54,
),
overflow: TextOverflow.ellipsis,
),
Text(
'user status',
style: TextStyle(
color: Colors.grey,
fontSize: 14.0,
),
)
],
),
)
],
),
SizedBox(
height: 10.0,
),
Container(
width: MediaQuery.of(context).size.width,
//height: 200.0,
child: ClipRRect(
borderRadius: BorderRadius.circular(8.0),
child: AspectRatio(
aspectRatio: 16 / 9,
child: FadeInImage.assetNetwork(
width: MediaQuery.of(context).size.width,
placeholder: 'assets/images/loading.jpg',
image: '${myPosts['post_image']}',
fit: BoxFit.fill,
),
),
),
),
SizedBox(
height: 10.0,
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Flexible(
child: Padding(
padding: const EdgeInsets.only(right: 8.0),
child: Text(
'${myPosts['post_title']}',
style: TextStyle(
fontSize: 18.0,
fontWeight: FontWeight.bold,
color: Colors.black87,
),
maxLines: 2,
overflow: TextOverflow.ellipsis,
),
),
),
FlatButton(
child: Text(
'Download Pdf',
style: TextStyle(
fontSize: 14.0,
color: Colors.black54,
),
),
shape: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.black54, width: 1),
borderRadius: BorderRadius.circular(5),
),
padding: const EdgeInsets.fromLTRB(
15.0,
4.0,
15.0,
4.0,
),
onPressed: () {},
),
],
),
SizedBox(
height: 10.0,
),
Text(
'${myPosts['post_body']}',
style: TextStyle(
fontSize: 16.0,
),
overflow: TextOverflow.ellipsis,
maxLines: 8,
),
SizedBox(
height: 10.0,
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text(
'Posted ' +
TimeAgo.getTimeAgo(
myPosts['post_time']),
style: TextStyle(color: Colors.grey)),
Text(
'${myPosts['comments_count']} Comment(s)',
style: TextStyle(color: Colors.grey)),
],
),
SizedBox(
height: 10.0,
),
],
),
),
),
),
);
},
);
}
return Loading();
},
),
一切正常,问题在于提取用户信息。我有一个 class 用于在个人资料页面上显示用户数据,如果它在这种情况下可能有用,下面是它的代码
import 'package:cloud_firestore/cloud_firestore.dart';
class ProfileService {
getProfileInfo(String uid) {
return Firestore.instance
.collection('Users')
.where('user_id', isEqualTo: uid)
.getDocuments();
}
}
我被卡住了,非常感谢你的帮助。
尝试使用这个:
Future<DocumentSnapshot> _getDocument(String uid) async {
return await Firestore().collection('Users').where('user_id', isEqualTo: uid).get();
}
它将return记录快照,然后您可以使用它来获取用户信息