0

我是来自 java(android) 的飞镖和颤振的新手。我有一个帖子列表,由不同的用户发布。我想为每个帖子显示用户名和用户图像。问题是我将用户存储在“用户”集合中,并将帖子存储在“帖子”集合中。我在每个帖子上都附加了用户 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();
  }
}

非常感谢您的帮助我被困住了。

4

1 回答 1

0

尝试使用这个:

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

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

于 2020-04-07T00:51:08.677 回答