Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to transform the method from Firestore to Realtime Database

im trying to convert database from firestore to firebase rtdb, by the tutorial I see the following code:

  static Stream<List<Message>> getMessages(String idUser) =>
      FirebaseFirestore.instance
          .collection('chats/$idUser/messages')
          .orderBy(MessageField.createdAt, descending: true)
          .snapshots()
          .transform(Utils.transformer(Message.fromJson));

here is the code I updated:

    static Stream<List<Message>> getMessages(String idUser) =>
  FirebaseDatabase.instance
      .ref()
      .child('chats/$idUser/messages')
      .onValue
      .map((message) =>
      message.snapshot.children.map((e) =>
          Message.fromJson(e.value as Map<String, dynamic>)).toList());

is it correct and how to add method orderBy, or please share me the correct code which has same value as the one for firestore, thanks a lot!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Firebase Realtime Database only support ascending ordering, so you can map .orderBy(MessageField.createdAt, descending: true) from Firestore to Realtime Database.

The closest you can get is:

FirebaseDatabase.instance
    .ref()
    .child('chats/$idUser/messages')
    .orderByChild(MessageField.createdAt)
    .onValue
    ...

And then reversing the results in your application code.

Also see the FlutterFire documentation on querying Realtime Database.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading