skip to Main Content

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"

2

Answers


  1. 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?

    Login or Signup to reply.
  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search