Flutter,如何检索用户信息并将其显示在 firestore 的特定博客文章中?

弗兰克弗朗西斯科

我是来自 java(android) 的 dart 和 flutter 新手。我有一个帖子列表,这些帖子由不同的用户发布。我想为每个帖子显示用户名和用户图像。问题是我将用户存储在“用户”集合中,并将帖子存储在“帖子”集合中。我在每个帖子上都附有用户 ID,我可以将其(ID)与帖子详细信息一起提取。但我不能用它(id)来显示个人资料信息。请帮助,一些代码行给我一些启发将不胜感激。波纹管是我显示帖子的代码:

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();
        },
      ),

一切正常,问题只是提取用户信息。我有一个类,我用来在个人资料页面上显示用户数据,如果它在这种情况下可能有用的话,下面是它的代码

import 'package:cloud_firestore/cloud_firestore.dart';

class ProfileService {
  getProfileInfo(String uid) {
    return Firestore.instance
        .collection('Users')
        .where('user_id', isEqualTo: uid)
        .getDocuments();
  }
}

您的帮助将不胜感激我被困住了。

GJJ2019

尝试使用这个:

Future<DocumentSnapshot> _getDocument(String uid) async {
 return await Firestore().collection('Users').where('user_id', isEqualTo: uid).get();
}

它将返回您的文档快照,然后您可以使用它来获取用户信息

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在flutter中从Firestore检索当前用户信息

使用Firestore进行简单的博客文章显示

如何使用flutter从Firestore中检索特定的用户详细信息

如何在Django REST Api中列出特定用户的博客文章

变量未显示在Rails博客文章中

使用 Wagtail 在博客文章中显示字数

Firestore:Javascript:如何将用户输入转换为数组并将其存储在 Firestore 中?

如何从博客文章页面中删除 Yoast

如何在博客文章中包含链接

Flutter:使用共享首选项从 firestore 保存用户 ID 并将值检索到类中

如何从 Firestore 检索数据并将其存储在全局变量中

如何在博客文章详细信息中添加表单?

显示特定博客文章独有的评论

如何从博客文章中获取用户名?

如何从 firestore 中获取文档集合并将其转换为 flutter 中的列表?

如何从Firestore中以双格式获取数据(数字)并将其乘以flutter

如何使用 Firestore 从在 Flutter 中存储用户 uid 的数组列表中访问用户信息

如何在Firestore中获取单个文档并将其显示在站点上

如何直接链接到博客文章或在线文章的特定部分?(例如到段落或标题)

TYPO3 博客文章列表中显示的元素

在首页Wordpress中显示某个分类的最新博客文章

如何在Bootstrap WordPress中添加2列博客文章

如何在博客文章中嵌入代码和图像

如何使用模型表单更新django中的博客文章?

如何在Slim 2中对博客文章进行分页?

使用 Flutter 创建用户集合后,如何在 Firestore 中检索文档 ID

如何在版本4.8.2中解决博客文章的WordPress文章永久链接结构?

从Firestore检索数据并在GridView.builder flutter中显示

Flutter-如何从Firestore检索ID列表并在GridView中显示其项目?