Satyam Goyal
Satyam Goyal

Reputation: 99

I want to fetch current user data from firebase database in my flutter application

My Question is that i want to fetch data of the current user only. but this code below is fetching data of all the users present in my Database. how can i fetch the data of only and only current user.

  1. This is the code with which i am fetching data from firebase(I am using Realtime DataBase). in this 'node-name' is the field under which my data is being stored.
class ShowDataPage extends StatefulWidget {
  @override
  _ShowDataPageState createState() => _ShowDataPageState();
}

class _ShowDataPageState extends State<ShowDataPage> {
  List<myData> allData = [];

  @override
  void initState() {
    DatabaseReference ref = FirebaseDatabase.instance.reference();
    ref.child('node-name').once().then((DataSnapshot snap) {
      var keys = snap.value.keys;
      var data = snap.value;
      allData.clear();
      for (var key in keys) {
        myData d = new myData(
          data[key]['name'],
          data[key]['message'],
          data[key]['profession'],
        );
        allData.add(d);
      }
      setState(() {
        print('Length : ${allData.length}');
      });
    });
  }
  1. This is the code from which i am uploading my data to the firebase under the name of 'node-name'. this code is stored in another file and is having another necessary fields also but this is the field which uploads my data to the firebase.
_sendToServer() {
    if (_key.currentState.validate()) {
      _key.currentState.save();
      DatabaseReference ref = FirebaseDatabase.instance.reference();
      var data = {
        "name": name,
        "profession": profession,
        "message": message,
      };
      ref.child('node-name').push().set(data).then((v) {
        _key.currentState.reset();
      });
    } else {
      setState(() {
        _autovalidate = true;
      });
    }
  }
  1. My data base in firebase looks like given below.

enter image description here

Upvotes: 0

Views: 1890

Answers (2)

aldobaie
aldobaie

Reputation: 1407

Use the user uid:

ref.child('node-name').child("M5CCSXQo3Upq5OC7y3lw").once()
.then((DataSnapshot snap) {...}

If you don't know the uid and didn't use it, then perform a query by the name fore example.

@override
void initState() {
    FirebaseAuth.instance.currentUser().then((user){
      fetchUser(user);
    });
}

fetchUser(FirebaseUser user)
{
  DatabaseReference ref = FirebaseDatabase.instance.reference();
  ref.child('node-name').child(user.uid).once().then((DataSnapshot snap) {
      var keys = snap.value.keys;
      var data = snap.value;
      allData.clear();
      for (var key in keys) {
        myData d = new myData(
          data[key]['name'],
          data[key]['message'],
          data[key]['profession'],
        );
        allData.add(d);
      }
      setState(() {
        print('Length : ${allData.length}');
      });
    });
}

Upvotes: 2

FurkanKURT
FurkanKURT

Reputation: 555

you can use like this

...
ref.child('node-name').child('/** current_user_key **/').once()
...

Upvotes: 1

Related Questions