1

I'm using flutter to query the database. I have a real-time database as below: enter image description here

I want to select a record by condition: email='test@gmail.com' and password='111111'

I read the solution following link Query based on multiple where clauses in Firebase But it does not compatible with flutter. My code looks like:

var snapshot = await _firestore
      .reference()
      .child(USERS_TBL)
      .orderByChild('email')
      .equalTo(email)
      .once()
      .then((DataSnapshot snapshot) {
        print(snapshot.value);
  });

output:

{-LeXpDcLqkwS0c1lgP7O: {user_role: USER, password: 111111, gender: 1, last_name: abcee, first_name: abcd, email: test@gmail.com}}

Of course, I can parse value to get password value, but I think there is another solution which is better. How can I do in flutter? Thanks a lot!!!

Community
  • 1
  • 1
Mr Special
  • 1,576
  • 1
  • 20
  • 33
  • 1
    There is no better query. This is the way queries work with Realtime Database - you will need to read each entire child node that matches the query. If you want to minimize the amount of data fetched by the query, then separate each field into a different root node. – Doug Stevenson May 12 '19 at 05:13
  • Have you tried using the [https://pub.dev/packages/firebase_database](https://pub.dev/packages/firebase_database)? Also, it would be a little more helpful if you can provide [a minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). There are also available example like ["Explore Realtime Database In Flutter"](https://medium.com/flutterdevs/explore-realtime-database-in-flutter-c5870c2b231f) if you haven't visited yet. This could give you insight on implementation of the Realtime Database for Flutter. – MαπμQμαπkγVπ.0 Jan 26 '21 at 18:10

1 Answers1

1

I suggest using Firestore if you're looking for flexible queries. With Firestore, you can run a query for your use case like this:

FirebaseFirestore.instance
    .collection(USERS_TBL)
    .where('email', isEqualTo: email).snapshots()

This returns a QuerySnapshot of all Documents in the collection with the same email.

Omatt
  • 8,564
  • 2
  • 42
  • 144