简体   繁体   中英

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.

在此处输入图像描述

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}');
      });
    });
}

you can use like this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM