Bahij.Mik
Bahij.Mik

Reputation: 1484

Flutter - How to retrieve last item from firebase real time database?

I have a chat system, and i want to show the most recent chat message outside for the user to see before joining the chat. enter image description here
Is there a Firebase way to accomplish this without actually pulling the whole conversation and getting the last one?

Upvotes: 0

Views: 1568

Answers (1)

Peter Haddad
Peter Haddad

Reputation: 80914

You can use limitToLast() to get the last item in the database:

  /// Create a query with limit and anchor it to the end of the window.
  Query limitToLast(int limit) {
    assert(!_parameters.containsKey('limitToLast'));
    return _copyWithParameters(<String, dynamic>{'limitToLast': limit});
  }

https://github.com/FirebaseExtended/flutterfire/blob/8832111250c04a0fb2c0bc6ff1f90fd702f828f2/packages/firebase_database/lib/src/query.dart#L156

Upvotes: 2

Related Questions