I want to display My user name, phone number, etc. in the user profile when the user is logged in from firebase firestore. I have Firebase firestore Database and the collection name of firestore is 'users'.
In my project, I want to display current user information like name, email, Number, but I can't do it.
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