显示所有用户数据,想在我的个人资料部分显示刚刚记录的用户数据
Showing all user data, Want to show just logged user data in my profile section
我想在 crrentUser/loggedIn 用户数据中显示我的个人资料 Drawer 当我滑动它时,将显示 current/login 用户 'name, email, photos' 还会有其他一些东西,例如注销按钮等。但是当我登录时该用户仪表板中的任何用户。当我 click/slide 在我的抽屉上时,它显示了我存储在 Firestore 中 用户集合 下的所有用户数据。我只想显示 Current 用户个人资料数据,如姓名电子邮件等。但它显示了来自 Firestore[ 的所有 collection =34=]。目前,我在我的 firebase 中的 users 集合中有 3 个用户。问题是当我 slide/click 抽屉时,它在我的 抽屉 中显示所有 3users 数据。但我只想在 Drawer
中显示 current/login 用户数据
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(),
);
},
),
),
),
);
}
}
所以我认为问题出在我假设的 steamBuider 上。我认为 SteamBuilder 打印 集合 中的所有内容。因为我只需要使用 SreamBuilder 打印我的 current/login 用户,所以它从 users 集合,这就是问题发生的原因。
现在我更改为 FutureBuilder 并且现在可以正常工作了。
**我不确定 StreamBuilder 有什么问题,但如果我需要整个集合,那么我将使用 SteamBuilder 否则,如果我只需要Collection 下的特定 document 然后我将使用 FutureBuilder.
这是我的意见,如果有人能提供一些非常有帮助的想法
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');
},
),
),
),
);
}
}
我想在 crrentUser/loggedIn 用户数据中显示我的个人资料 Drawer 当我滑动它时,将显示 current/login 用户 'name, email, photos' 还会有其他一些东西,例如注销按钮等。但是当我登录时该用户仪表板中的任何用户。当我 click/slide 在我的抽屉上时,它显示了我存储在 Firestore 中 用户集合 下的所有用户数据。我只想显示 Current 用户个人资料数据,如姓名电子邮件等。但它显示了来自 Firestore[ 的所有 collection =34=]。目前,我在我的 firebase 中的 users 集合中有 3 个用户。问题是当我 slide/click 抽屉时,它在我的 抽屉 中显示所有 3users 数据。但我只想在 Drawer
中显示 current/login 用户数据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(),
);
},
),
),
),
);
}
}
所以我认为问题出在我假设的 steamBuider 上。我认为 SteamBuilder 打印 集合 中的所有内容。因为我只需要使用 SreamBuilder 打印我的 current/login 用户,所以它从 users 集合,这就是问题发生的原因。 现在我更改为 FutureBuilder 并且现在可以正常工作了。 **我不确定 StreamBuilder 有什么问题,但如果我需要整个集合,那么我将使用 SteamBuilder 否则,如果我只需要Collection 下的特定 document 然后我将使用 FutureBuilder.
这是我的意见,如果有人能提供一些非常有帮助的想法
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');
},
),
),
),
);
}
}