如何在Flutter中显示Firebase用户列表,但不包括已登录的用户?

huangapple go评论86阅读模式
英文:

How to display a list of Firebase users excluding the logged-in user in flutter?

问题

目前,我有一个SteamBuilder从“user”集合中获取数据。

return ListView.builder(
          itemCount: snapshot.data.documents.length,
          itemBuilder: (context, index) {
            DocumentSnapshot document = snapshot.data.documents[index];

            // 否则
            return user.uid == document['uid']
                ? null //null不应该返回,但是应该排除在下面的ListTile中
                // 这导致只显示到已登录用户的用户,然后终止而不呈现其他小部件
                
                : ListTile(
                    contentPadding: EdgeInsets.all(10),
                    title: Text(
                      document['name'],
                      style: TextStyle(
                          fontSize: 30,
                          fontWeight: FontWeight.bold,
                          fontStyle: FontStyle.italic),
                    ),
                  );
          });
    },
  ),

我应该实现什么逻辑以排除已登录用户的渲染,但显示所有其他用户,我可以将一个where()传递给snapshot吗?

英文:

Currently, I have a SteamBuilder fetching data from a "user" collection

return ListView.builder(
          itemCount: snapshot.data.documents.length,
          itemBuilder: (context, index) {
            DocumentSnapshot document = snapshot.data.documents[index];

            // else
            return user.uid == document['uid']
                ? null //null should not be returned but the user should be excluded in the below ListTile
                // this is causing only the users up to the logged-in user to display and then terminating without rendering other widgets
                
                : ListTile(
                    contentPadding: EdgeInsets.all(10),
                    title: Text(
                      document['name'],
                      style: TextStyle(
                          fontSize: 30,
                          fontWeight: FontWeight.bold,
                          fontStyle: FontStyle.italic),
                    ),
                  );
          });
    },
  ),

What logic should I implement so I exclude the logged-in user from rendering but display all others, can I pass a where() to the snapshot?

答案1

得分: 0

以下是您要翻译的内容:

return ListView.separated(
  itemBuilder: (context, index) {
    return InkWell(
      onTap: () {},
      child: Container(
        height: snapshot.data.documents[index].data['id'] != uid
            ? 70.0
            : 0.0, // this line for hiding specific cell...
        width: MediaQuery.of(context).size.width,
        child: Column(
          mainAxisSize: MainAxisSize.max,
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Row(
              mainAxisAlignment: MainAxisAlignment.start,
              mainAxisSize: MainAxisSize.max,
              children: <Widget>[
                Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: Text(
                    snapshot.data.documents[index].data['nickname'],
                  ),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  },
  separatorBuilder: (context, index) {
    return snapshot.data.documents[index].data['id'] != uid
        ? Divider()
        : Container(); // this line hide the devider for specific cell..
  },
  itemCount: snapshot.data.documents.length,
);
英文:

> I am facing the same problem but here is the solution in below sample code I hope this will work for you.

return ListView.separated(
            itemBuilder: (context, index) {
              return InkWell(
                onTap: () {},
                child: Container(
                  height: snapshot.data.documents[index].data[&#39;id&#39;] != uid
                      ? 70.0
                      : 0.0, // this line for hiding specific cell...
                  width: MediaQuery.of(context).size.width,
                  child: Column(
                    mainAxisSize: MainAxisSize.max,
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: &lt;Widget&gt;[
                      Row(
                        mainAxisAlignment: MainAxisAlignment.start,
                        mainAxisSize: MainAxisSize.max,
                        children: &lt;Widget&gt;[
                          Padding(
                              padding: const EdgeInsets.all(8.0),
                              child: Text(snapshot
                                  .data.documents[index].data[&#39;nickname&#39;]))
                        ],
                      )
                    ],
                  ),
                ),
              );
            },
            separatorBuilder: (context, index) {
              return snapshot.data.documents[index].data[&#39;id&#39;] != uid
                  ? Divider()
                  : Container(); // this line hide the devider for specific cell..
            },
            itemCount: snapshot.data.documents.length);

huangapple
  • 本文由 发表于 2020年1月4日 01:10:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/59582583.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定