0

Here I am trying to retrive data from realtime database in flutter

Future<UserModel> getUserDrtails(String email) async{
    final snapshot = await _db.collection("User").where("Email", isEqualTo: email).get();
    final userData = snapshot.docs.map((e)=>UserModel.fromSnapshot(e)).single;
    return userData;

  }

Here data is retrived from firestore database but I am using realtime database so what I have to change to retrive data from realtime database. While using realtime database this code gives error at "collection"

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807

2 Answers2

0

While Firestore and the Realtime Database are both part of Firebase, they are completely separate and each have their own API.

To get started with the Firebase Realtime Database, have a look here. You'll indeed find that Realtime Database doesn't have a concept of collections nor of documents, but instead has a data model that is one bug tree of JSON data. So you'll want to modify your data model to fit that.

For more on this and other changes, also see What's the difference between Cloud Firestore and the Firebase Realtime Database?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
0

Use something like this for realtime database:

final ref = FirebaseDatabase.instance.ref();
final snapshot = await ref.child('users/$userId').get();
if (snapshot.exists) {
    print(snapshot.value);
} else {
    print('No data available.');
}

Refer: https://firebase.flutter.dev/docs/database/read-and-write

krishnaacharyaa
  • 14,953
  • 4
  • 49
  • 88
  • While this is valid code, it doesn't provide a solution to the question - the OP is *querying* the Firestore database for any documents that have a given email address in the Email field. This code doesn't do that - it reads a specific node from the Realtime Database. Still good code but it would be a better answer if it performed the same query. – Jay Jan 05 '23 at 18:35