0

I tried this way, but i'm getting an error.

The error:

The method 'data' isn't defined for the type 'CollectionReference'. (undefined_method at [myapp] android\app\lib\useracc.dart:32)

void getData() async{
    User? user = await FirebaseAuth.instance.currentUser;
        var vari =FirebaseFirestore.instance.collection("users");

        setState (() {
          name = vari.data()['firstname'];
        }
        );

  }

Signup/Register Page

Future<User?> _register(String fname,String lname ,String email, String password) async{
    FirebaseAuth _auth = FirebaseAuth.instance;

    FirebaseFirestore _firestore = FirebaseFirestore.instance;
    try {
      UserCredential userCrendetial =  await _auth.createUserWithEmailAndPassword(email: emailController.text, password: passwordController.text);

      print("Account created Succesfull");

      userCrendetial.user!.updateDisplayName(fname);
      userCrendetial.user!.updateDisplayName(lname);

      await _firestore.collection('users').doc(_auth.currentUser!.uid).set({
        "firstname": fname,
        "lastname" : lname,
        "email": email,
        "uid": _auth.currentUser!.uid,
      });

      return userCrendetial.user;
    } catch (e) {
      print(e);
      return null;
    }
  }

This is the user account from where i want to fetch info:

Please help. I'm struck here a long time.

2 Answers 2

1

You should retrieve the currentUser document then access its data:

void getData() async{
    var vari = await FirebaseFirestore.instance
            .collection("users")
            .doc(FirebaseAuth.instance.currentUser.uid)
            .get();

    setState (() {
      name = vari.data()['firstname'];
    });
}
Sign up to request clarification or add additional context in comments.

1 Comment

Hey, my page is blank, it is not showing anything, not even an error.
1

if you've saved your user's details in firestore and its document id is the same as that of user ID (which is preferred for ease of access and control), then:

var vari =FirebaseFirestore.instance.collection("users").doc(user!.uid).get();

This gets the document of the user, and the type is DocumentSnapshot.

Map<String,dynamic> userData = vari as Map<String,dynamic>;

now userData is stored in form of Map. suppose you want to access their 'name', so the syntax now goes like userData['name'].

Similarly other fields can be accessed from variable. It's preferred to store userData in a Provider to access it's contents anywhere in your app.

Heres a sample database structure

Full code snippet

void getData() async{
    User? user = await FirebaseAuth.instance.currentUser;
        var vari =FirebaseFirestore.instance.collection("users").doc(user!.uid).get();
Map<String,dynamic> userData = vari as Map<String,dynamic>;

        setState (() {
          name = userData['firstname']; //or name = userData['name']
        }
        );

  }

12 Comments

I'm new to Flutter. I will post the code from my register page.
sure, ill try to help you out
@QueenBee yes, my code should work, give it a try.
Did it work? It'd be great if you could accept the answer if it did.
It's not showing any result. My screen is white as a sheet.
|

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.