首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >显示所有用户数据,希望在“我的配置文件”部分中只显示已记录的用户数据。

显示所有用户数据,希望在“我的配置文件”部分中只显示已记录的用户数据。
EN

Stack Overflow用户
提问于 2021-10-31 08:56:15
回答 1查看 234关注 0票数 0

我想将我的crrentUser/登录用户数据显示在抽屉中,当我滑动它时,它将显示当前/登录用户的名称、电子邮件、照片‘,还有其他一些东西,比如注销按钮等等。但是当我与该用户仪表板中的任何用户一起登录时。当我在抽屉上单击/幻灯片时,它会显示我存储在Firestore中的用户集合下的所有用户数据。我只想显示当前的用户配置文件数据,如姓名、电子邮件等,但是它显示了来自Firestore的所有集合。目前,我的用户集合中有3用户,在我的firebase中。问题是当我滑动/单击抽屉时,它会在我的抽屉中显示所有的3用户的数据。但我只想在我的current/login抽屉中显示用户数据。

代码语言:javascript
运行
复制
import 'package:cloud_firestore/cloud_firestore.dart';

import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart';
import '../screens/user_product_list_view.dart';

class UserDrawer extends StatefulWidget {
  UserDrawer({Key? key}) : super(key: key);

  @override
  _UserDrawerState createState() => _UserDrawerState();
}

class _UserDrawerState extends State<UserDrawer> {
  @override
  void initState() {
    super.initState();
    getUser();
  }

  Future getUser() async {
    var currentUserLoginUser = await FirebaseAuth.instance.currentUser;
    var firebaseUser = await FirebaseFirestore.instance
        .collection('users')
        .doc(currentUserLoginUser!.uid);
    print(currentUserLoginUser.email);
  }

  @override
  Widget build(BuildContext context) {
    return Drawer(
      child: SafeArea(
        child: Scaffold(
          backgroundColor: Theme.of(context).primaryColor,
          body: StreamBuilder(
            stream: FirebaseFirestore.instance.collection('users').snapshots(),
            builder: (BuildContext contex,
                AsyncSnapshot<QuerySnapshot<Map<String, dynamic>>> snapshot) {
              if (!snapshot.hasData) {
                return Text('User is not found');
              }
              return ListView(
                children: snapshot.data!.docs.map(
                  (document) {
                    return Stack(
                      children: [
                       ///
                        Container(
                          padding: EdgeInsets.only(top: 105),
                          height: 300,
                          width: double.infinity,
                          decoration: BoxDecoration(
                              color: Theme.of(context).primaryColorDark,
                              borderRadius: BorderRadius.only(
                                  bottomLeft: Radius.circular(120))),
                          child: Column(
                            children: [
                              CircleAvatar(
                                radius: 40,
                                backgroundColor: Colors.blueAccent,
                              ),
                              Text(
                                document['email'],
                                style: TextStyle(
                                    fontWeight: FontWeight.bold, fontSize: 20),
                              ),
                            ],
                          ),
                        ),
                        Container(
                          padding: EdgeInsets.symmetric(
                              horizontal: 20, vertical: 10),
                          height: 100,
                          width: double.infinity,
                          child: Text(
                            'Profile',
                            style: TextStyle(
                              fontSize: 24,
                              fontWeight: FontWeight.bold,
                            ),
                          ),
                          decoration: BoxDecoration(
                              color: Theme.of(context).primaryColorLight,
                              borderRadius: BorderRadius.only(
                                  bottomLeft: Radius.circular(80))),
                        ),
                      ],
                    );
                  },
                ).toList(),
              );
            },
          ),
        ),
      ),
    );
  }
}

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-10-31 09:51:19

所以我认为问题出在steamBuider上,我是这样认为的。我认为SteamBuilder集合中打印所有东西。由于我只需要通过使用SreamBuilder打印当前/登录用户,所以需要从用户集合中获取所有文档数据,这就是问题出现的原因。现在我转到了FutureBuilder,现在工作得很好。**我不确定StreamBuilder有什么问题,但是如果我需要整个集合,那么如果我只需要一个特定的文档集合下,那么我将使用FutureBuilder.

,这是我的观点,如果有人能给出一些很有帮助的主意,

代码语言:javascript
运行
复制
import 'package:cloud_firestore/cloud_firestore.dart';

import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart';
import '../screens/user_product_list_view.dart';

class UserDrawer extends StatefulWidget {
  UserDrawer({Key? key}) : super(key: key);

  @override
  _UserDrawerState createState() => _UserDrawerState();
}

class _UserDrawerState extends State<UserDrawer> {
  @override
  void initState() {
    super.initState();
    getUser();
  }

  var currentUserLoginUser = FirebaseAuth.instance.currentUser;
  Future getUser() async {
    var currentUserLoginUser = await FirebaseAuth.instance.currentUser;
    var firebaseUser = await FirebaseFirestore.instance
        .collection('users')
        .doc(currentUserLoginUser!.uid);
    print(currentUserLoginUser.email);
  }

  @override
  Widget build(BuildContext context) {
    return Drawer(
      child: SafeArea(
        child: Scaffold(
          backgroundColor: Theme.of(context).primaryColor,
          body: FutureBuilder<DocumentSnapshot>(
            future: FirebaseFirestore.instance
                .collection('users')
                .doc(currentUserLoginUser!.uid)
                .get(),
            builder: (BuildContext context,
                AsyncSnapshot<DocumentSnapshot> snapshot) {
              if (snapshot.hasError) {
                return Text("Something went wrong");
              }
              if (snapshot.hasData && !snapshot.data!.exists) {
                return Text("Document does not exist");
              }
              if (snapshot.connectionState == ConnectionState.done) {
                Map<String, dynamic> data =
                    snapshot.data!.data() as Map<String, dynamic>;
                return Stack(
                  children: [
                    Container(
                      padding: EdgeInsets.only(top: 600),
                      decoration: BoxDecoration(
                          color: Theme.of(context).primaryColorLight,
                          borderRadius: BorderRadius.only(
                              bottomRight: Radius.circular(80))),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        crossAxisAlignment: CrossAxisAlignment.center,
                        children: [
                          ElevatedButton(
                              onPressed: () {
                               FirebaseAuth.instance.signOut();
                              },
                              child: Text('Logout')),
                        ],
                      ),
                    ),
                    Container(
                      padding: EdgeInsets.only(top: 400),
                      decoration: BoxDecoration(
                          color: Theme.of(context).primaryColorLight,
                          borderRadius: BorderRadius.only(
                              bottomLeft: Radius.circular(80))),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        crossAxisAlignment: CrossAxisAlignment.center,
                        children: [
                          Expanded(
                              child: ElevatedButton(
                                  onPressed: () {
                                    Navigator.push(
                                        context,
                                        MaterialPageRoute(
                                            builder: (context) =>
                                                UserProductList()));
                                  },
                                  child: Text('My Products'))),
                          SizedBox(
                            width: 08,
                          ),
                          Expanded(
                              child: ElevatedButton(
                                  onPressed: () {}, child: Text('data'))),
                        ],
                      ),
                    ),
                    Container(
                      padding: EdgeInsets.only(top: 105),
                      height: 300,
                      width: double.infinity,
                      decoration: BoxDecoration(
                          color: Theme.of(context).primaryColorDark,
                          borderRadius: BorderRadius.only(
                              bottomLeft: Radius.circular(120))),
                      child: Column(
                        children: [
                          CircleAvatar(
                            radius: 40,
                            backgroundColor: Colors.blueAccent,
                          ),
                          Text(
                            ' Name: ${data['username']} ',
                            style: TextStyle(
                                fontWeight: FontWeight.bold, fontSize: 20),
                          ),
                        ],
                      ),
                    ),
                    Container(
                      padding:
                          EdgeInsets.symmetric(horizontal: 20, vertical: 10),
                      height: 100,
                      width: double.infinity,
                      child: Text(
                        'Profile',
                        style: TextStyle(
                          fontSize: 24,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                      decoration: BoxDecoration(
                          color: Theme.of(context).primaryColorLight,
                          borderRadius: BorderRadius.only(
                              bottomLeft: Radius.circular(80))),
                    ),
                  ],
                );
              }
              return Text('Loading');
            },
          ),
        ),
      ),
    );
  }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69785290

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档