9

Today I started experimenting with Firebase Live database. Maybe I'm thinking too much in sql terms. But what I want to do is get a record from my database where a value equals a variable with flutter. My table looks like this:

enter image description here

What I'm trying to achieve is something like this:

FirebaseDatabase.instance.reference().child('users').where('User_id', 1508)

Like I said. I'm a complete beginner when it comes to Live Databases. I hope someone can help me with this issue I'm having.

Kind regards, Namanix

KENdi
  • 7,576
  • 2
  • 16
  • 31
Kevin Walter
  • 6,507
  • 8
  • 28
  • 32

3 Answers3

8

According to firstore docs

Firestore.instance
  .collection('talks')
  .where("topic", isEqualTo: "flutter")
  .snapshots()
  .listen((data) =>
    data.documents.forEach((doc) => print(doc["title"])));
Nomi
  • 710
  • 3
  • 11
  • 21
  • This seems correct, but calling those "docs" is very generous. The Firebase documentation for Dart/Flutter is so bad that I question if soon they'll drop support altogether... I'm scared to proceed with this project. If they were more serious about dart/flutter support wouldn't they give more attention to their docs? – Tyler Biscoe Aug 30 '19 at 02:38
  • 1
    You are talking about the FirebaseStore but question is related to the real-time-database. – Sohaib Aslam Feb 03 '20 at 07:19
  • how can we use your code in widgets like whether to wrap in future builder and what to use `QuerySsnapshots` or `DocumentSnapshot` – Mithson Sep 24 '21 at 16:29
2

If you have the user id in a variable for example called:

String uid = currentUser.uid;

then you can do the following:

FirebaseDatabase.instance.reference().child('users/$uid')

Update I think this is what you are asking about

FirebaseDatabase.instance
                    .reference().child("users")
                    .orderByChild("User_id")
                    .equalTo($this.userId)
Shady Aziza
  • 50,824
  • 20
  • 115
  • 113
0
@override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
          child: FutureBuilder<QuerySnapshot>(
        future: FirebaseFirestore
                .instance
                .collection('users') 
                .where("user_id", isEqualTo: FirebaseAuth.instance.currentUser!.uid)//  Your where condition here
                .get(), 
        builder: (_, snapshot) {
          if (snapshot.hasError) return Text('Error = ${snapshot.error}');
          if (snapshot.connectionState == ConnectionState.waiting) {
            return const Text("Loading");
          }
          return ListView(
              children: snapshot.data!.docs.map((DocumentSnapshot document) {
            Map<String, dynamic> data = document.data()! as Map<String, dynamic>;
            return ListTile(
              title: Text(data['avatar']), //  Your valid data here
            );
          }).toList());
        },
      )),
    );
  }

Also refer: How to use StreamBuilder and FutureBuilder for single and multiple documents

krishnaacharyaa
  • 14,953
  • 4
  • 49
  • 88