【问题标题】:Flutter with Firebase Realtime Database, how to displaying the name of the user who made a postFlutter with Firebase Realtime Database,如何显示发帖用户的姓名
【发布时间】:2022-08-14 19:19:30
【问题描述】:

我正在使用颤振和 Firebase Realtime 创建一个应用程序,其中我具有用户可以发布文章的功能。我想要列出所有这些文章以及发布它的用户的名称。但是如何显示他的名字,知道在数据库级别它只是记录发布帖子的人的 ID? 谢谢

这是相关数据库的结构:

我对 Firebase Realtime 的请求中的代码:

ref = FirebaseDatabase.instance.reference().child(\"post\").orderByKey();
FirebaseAnimatedList(
  scrollDirection: Axis.vertical,
  shrinkWrap: true,
  query: ref,
  itemBuilder: (BuildContext context, DataSnapshot snapshot,
      Animation<double> animation, int index) {
    return Container(
      padding: EdgeInsets.all(10),
      margin: EdgeInsets.only(bottom: 20),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: <Widget>[
              Row(
                children: <Widget>[
                  Container(
                    width: 50,
                    height: 50,
                    decoration: BoxDecoration(
                        shape: BoxShape.circle,
                        image: DecorationImage(
                            image: NetworkImage(snapshot
                                .value[\"imagePost\"]
                                .toString()),
                            fit: BoxFit.cover)),
                  ),
                  SizedBox(
                    width: 10,
                  ),
                  Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: <Widget>[
                      RichText(
                        text: TextSpan(
                          //text: snapshot.value[\"user_id\"].toString(),
                          text: \"\" +
                              FirebaseDatabase.instance
                                  .reference()
                                  .child(\'users\')
                                  .child(
                                      \"${snapshot.value[\"user_id\"].toString()}\")
                                  .child(\'nom\')
                                  .toString(),
                          style: TextStyle(
                              color: Colors.grey[900],
                              fontSize: 14,
                              fontWeight: FontWeight.bold,
                              letterSpacing: 1),
                        ),
                      ),
                      SizedBox(
                        height: 3,
                      ),
                      Text(
                        \"${timeago.format(DateTime.parse(\"${DateTime.fromMillisecondsSinceEpoch(snapshot.value[\'createAt\'])}\"), locale: \'fr_short\')}\",
                        style: TextStyle(
                            fontSize: 12, color: Colors.grey),
                      ),
                    ],
                  )
                ],
              ),
            ],
          ),
          SizedBox(
            height: 20,
          ),
          RichText(
            text: TextSpan(
              text: snapshot.value[\"description\"].toString(),
              style: TextStyle(
                  fontSize: 12,
                  color: Colors.grey[800],
                  height: 1.5,
                  letterSpacing: .7),
            ),
          ),
          SizedBox(
            height: 20,
          ),
          snapshot.value[\"imagePost\"].toString() != \'\'
              ? Container(
                  height: 200,
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(10),
                    image: DecorationImage(
                        image: NetworkImage(
                            snapshot.value[\"imagePost\"].toString()),
                        fit: BoxFit.cover),
                  ),
                )
              : Container(
                  height: 200,
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(10),
                    color: kPrimaryColor,
                  ),
                ),
          SizedBox(
            height: 20,
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: <Widget>[
              makeLikeButton(isActive: false),
              Padding(
                padding: EdgeInsets.only(
                  right: 10,
                ),
              ),
              Row(
                children: <Widget>[
                  makeLike(),
                  SizedBox(
                    width: 5,
                  ),
                  Text(
                    \"2.5K\",
                    style: TextStyle(
                        fontSize: 10, color: Colors.grey[800]),
                  )
                ],
              ),
            ],
          ),
        ],
      ),
    );
  },
),
  • 您是否将用户列表存储在数据库中的某个位置?\'
  • 是的,@FrankvanPuffelen

标签: flutter firebase firebase-realtime-database


【解决方案1】:

你找到问题的答案了吗?我有同样的问题,我从帖子中获得了用户 ID。现在我不知道如何查询用户详细信息并将它们显示在同一个列表视图中。 (颤振 - 实时数据库)

【讨论】:

  • 如果可能的话,你能帮助我吗?先感谢您
猜你喜欢
  • 2019-09-14
  • 2020-11-22
  • 1970-01-01
  • 1970-01-01
  • 2021-09-12
  • 1970-01-01
  • 1970-01-01
  • 2019-04-10
  • 1970-01-01
相关资源
最近更新 更多