3

Can you help me? I've only been learning Flutter for a few months and I want to get user data from Firebase and display the user profile. But it doesn't work, this is my code:

DatabaseReference _ref;

  var email, nama, noTelp;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();



    final User usernya = _auth.currentUser;
    final String uid = usernya.uid;

    _ref = FirebaseDatabase.instance.reference().child('user').child(uid);

    email = _ref.child('email');
    nama = _ref.child('nama');
    noTelp = _ref.child('noTelp');
  }

And this code for user profiles:

Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              _ref == null
                                  ? Text(
                                      'Nama Pengguna',
                                      style: TextStyle(
                                          fontSize: 18,
                                          fontWeight: FontWeight.bold,
                                          color: Colors.white),
                                    )
                                  : Text(
                                      nama.toString(),
                                      style: TextStyle(
                                          fontSize: 18,
                                          fontWeight: FontWeight.bold,
                                          color: Colors.white),
                                    ),
                              Text(
                                email.toString(),
                                style: (TextStyle(color: Colors.white)),
                              ),
                              Text(noTelp.toString(),
                                  style: (TextStyle(color: Colors.white)))
                            ],
                          )

And I get output like this instance of 'Database Reference'

This is my user data in firebase:

enter image description here

And I got output like this:

enter image description here

2 Answers 2

1

You should call Firestore method get() to fetch data, then read the returned document from the callback.

FirebaseDatabase.instance.reference().child('user').child(uid).get().then((docSnapshot) {
    print(docSnapshot.data()["email"]); // print data to test
  });

Please find references here

Sign up to request clarification or add additional context in comments.

1 Comment

The question is asking about Realtime Database, not Firestore. There is no get() available.
0

It should look like that (with cloud firestore):

  // collection reference
  final CollectionReference _userCollection =
      Firestore.instance.collection('users');
  
  Future getUser(String uid) async {
    return await _userCollection.document(uid).snapshots().map((doc) {
      _userDataFromSnapshot(doc);
    });
  }
  
  // userData from snapshot
  UserData _userDataFromSnapshot(DocumentSnapshot snapshot) {
    return UserData(
      uid: uid,
      nickname: snapshot.data['nickname'],
    );
  }

1 Comment

The question is asking about Realtime Database, not Firestore.

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.