0

I want to get the user name that is currently logged in from firebase's real-time databse. I stored the information on signing up the user. I used Future Builder and it is also printing the name on the terminal but I am unable to display the username on Navigation drawer. Kindly help me out where I am doing wrong as I am new to Flutter. Thank you in advance!

Code for function:

Future<String> getUsername() async {
    final ref = FirebaseDatabase.instance.reference();
    User cuser = await firebaseAuth.currentUser;

    ref.child('User_data').child(cuser.uid).once().then((DataSnapshot snap) {
      final String userName = snap.value['name'].toString();
      print(userName);
      return userName;
    });
  }

Code where i have used Builder:

Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    FutureBuilder<String>(
                      future: getUsername(),
                      builder: (context, snapshot) {
                        if (snapshot.hasData) {
                          return Text(
                            snapshot.data,
                            style: TextStyle(
                              fontFamily: 'Montserrat',
                              fontSize: 13,
                              color: const Color(0xff000000),
                              fontWeight: FontWeight.w700,
                            ),
                            textAlign: TextAlign.left,
                          );
                        } else {
                          return Text(
                            "Loading data...",
                            style: TextStyle(
                              fontFamily: 'Montserrat',
                              fontSize: 13,
                              color: const Color(0xff000000),
                              fontWeight: FontWeight.w700,
                            ),
                            textAlign: TextAlign.left,
                          );
                        }
                      },
                    ),

Image of my Screen:

enter image description here

My Database where information is stored:

I want to get this 2nd user's name: Hania Salman from the database

enter image description here

0

1 Answer 1

1

You aren't returning the username from your getUsername() function. The return userName you have written is for the function inside then() not for getUsername(). So it should be like this

Future<String> getUsername() async {
    final ref = FirebaseDatabase.instance.reference();
    User cuser = await firebaseAuth.currentUser;

    return ref.child('User_data').child(cuser.uid).once().then((DataSnapshot snap) {
      final String userName = snap.value['name'].toString();
      print(userName);
      return userName;
    });
  }
Sign up to request clarification or add additional context in comments.

3 Comments

isn't this and the one I have written are the same, tho it's working but I cannot figure out the difference?
i have just added the return statement you were missing at line 3 in the getusername function....
oh okay, I get it Thank you very much :)

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.