1

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

3
  • 1
    try to print your variables in the then code block to know if they exist or not ! Commented Dec 12, 2022 at 5:52
  • @Gwhyyyi try it but nothing is print Commented Dec 12, 2022 at 6:31
  • then you should double check you firestore get() call, make sure you didn't miss a letter, space, underscore in the collection name, and try to print the user uid in the debug log and compare it with the document's name in your collection Commented Dec 12, 2022 at 6:34

1 Answer 1

1
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']),
        ),
        ],
       ),
     ),
    );
    }),
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.