[英]How to display current User data from firebase?
I want to display My user name, phone number, etc. in the user profile when the user is logged in from firebase firestore.当用户从 firebase firestore 登录时,我想在用户配置文件中显示我的用户名、电话号码等。 I have Firebase firestore Database and the collection name of firestore is 'users' .
我有 Firebase firestore 数据库,firestore 的集合名称是'users' 。
In my project, I want to display current user information like name, email, Number, but I can't do it.在我的项目中,我想显示当前用户信息,如姓名、email、号码,但我做不到。
When I run my app the name show blank.当我运行我的应用程序时,名称显示为空白。
here is my code:这是我的代码:
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:gtu_all_in_one/Pages/FirstPage.dart';
import 'package:hexcolor/hexcolor.dart';
import 'package:provider/provider.dart';
import '../Providers/SignInProvider.dart';
import '../Utils/NextScreen.dart';
import '../models/QrCodeScannerPage.dart';
class ProfilePage extends StatefulWidget {
const ProfilePage({Key? key}) : super(key: key);
@override
State<ProfilePage> createState() => _ProfilePageState();
}
class _ProfilePageState extends State<ProfilePage> {
String? name = " ";
String? email;
final currentUser = FirebaseAuth.instance;
Future _getDataFromDatabase() async {
await FirebaseFirestore.instance
.collection('users')
.doc(FirebaseAuth.instance.currentUser!.uid)
.get()
.then((snapshot) async {
if (snapshot.exists) {
setState(() {
name = snapshot.data()!["name"];
email = snapshot.data()!["Email"];
});
}
});
}
@override
void initState() {
super.initState();
_getDataFromDatabase();
}
@override
Widget build(BuildContext context) {
// for show data on screen
final sp = context.watch<SignInProvider>();
return SafeArea(
child: Scaffold(
resizeToAvoidBottomInset: false,
body: Center(
child: Column(
children: [
SizedBox(height: 300),
//here i display name of current user
Text("Name is : $name"),
SizedBox(height: 10),
Container(
height: 40,
width: 70,
color: Colors.greenAccent,
child: TextButton(
onPressed: () {
print("name is :" + name.toString());
},
child: Text("Btn"),
),
),
SizedBox(height: 10),
//for Opening QR Code Scanner
SizedBox(
height: 50,
width: 250,
child: ElevatedButton(
style: ElevatedButton.styleFrom(
primary: HexColor("#48EDC5"),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(22),
),
),
child: Row(
children: [
//for text
Container(
margin: const EdgeInsets.only(left: 13),
child: Text(
"Mark Attendance",
style: TextStyle(
fontFamily: 'Gotham',
color: HexColor("#002C00"),
fontSize: 20),
),
),
//for Icone
Container(
margin: EdgeInsets.only(left: 5),
child: Icon(
Icons.qr_code_scanner,
size: 22,
color: HexColor("#002C00"),
),
),
],
),
//goto QR Code Scanner Page
onPressed: () {
nextScreen(context, QRCodeScanner());
},
),
),
SizedBox(height: 10),
//for LogOut Button
SizedBox(
height: 50,
width: 150,
child: ElevatedButton(
style: ElevatedButton.styleFrom(
primary: HexColor("#48EDC5"),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(22),
),
),
child: Row(
children: [
//for text
Container(
margin: const EdgeInsets.only(left: 13),
child: Text(
"log Out",
style: TextStyle(
fontFamily: 'Gotham',
color: HexColor("#002C00"),
fontSize: 20),
),
),
//for Icone
Container(
margin: EdgeInsets.only(left: 5),
child: Icon(
Icons.arrow_forward_ios,
size: 22,
color: HexColor("#002C00"),
),
),
],
),
//goto SignUp Page
onPressed: () {
sp.userSignOut();
nextScreenReplace(context, FirstPage());
},
),
),
],
),
),
),
);
}
}
Here is my O/P enter image description here这是我的 O/P在此处输入图片描述
StreamBuilder(
stream:
Firestore.instance.collection('userData').snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return CircularProgressIndicator();
}
return Container(
child: Padding(
padding: const EdgeInsets.only(top: 75.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
snapshot.data.documents[0]['userName'],
style: TextStyle(fontSize: 25.0),
),
Padding(
padding: const EdgeInsets.only(top: 5.0),
child:
Text(snapshot.data.documents[0]['email']),
),
],
),
),
);
}),
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.