@@ -43,6 +43,7 @@ class RealtimeDBPagination extends StatefulWidget {
4343 super .key,
4444 required this .query,
4545 required this .itemBuilder,
46+ required this .orderBy,
4647 this .separatorBuilder,
4748 this .limit = 10 ,
4849 this .viewType = ViewType .list,
@@ -75,6 +76,19 @@ class RealtimeDBPagination extends StatefulWidget {
7576 /// the item in the list.
7677 final Widget Function (BuildContext , DataSnapshot , int ) itemBuilder;
7778
79+ /// The field to use to sort the data. Give the same value as the field
80+ /// used to order the data in the query.
81+ ///
82+ /// ## Example
83+ /// If the query is:
84+ /// ```dart
85+ /// FirebaseDatabase.instance.ref('messages').orderByChild('createdAt')
86+ /// ```
87+ /// Then the value of [orderBy] should be `createdAt` .
88+ ///
89+ /// If null, the data will be sorted by the key.
90+ final String ? orderBy;
91+
7892 /// The builder to use to render the separator.
7993 ///
8094 /// Only used if [viewType] is [ViewType.list] .
@@ -190,7 +204,9 @@ class _RealtimeDBPaginationState extends State<RealtimeDBPagination> {
190204 final docsLimit = _data.length + (getMore ? widget.limit : 0 );
191205 var docsQuery = widget.query.limitToFirst (docsLimit);
192206 if (_data.isNotEmpty) {
193- docsQuery = docsQuery.startAt (null , key: _data.first.key);
207+ docsQuery = docsQuery.startAt (
208+ (_data.first.value as Map <String , dynamic >? )? [widget.orderBy],
209+ );
194210 }
195211
196212 _streamSub = docsQuery.onValue.listen ((DatabaseEvent snapshot) async {
@@ -234,7 +250,9 @@ class _RealtimeDBPaginationState extends State<RealtimeDBPagination> {
234250
235251 var latestDocQuery = widget.query.limitToFirst (1 );
236252 if (_data.isNotEmpty) {
237- latestDocQuery = latestDocQuery.endBefore (null , key: _data.first.key);
253+ latestDocQuery = latestDocQuery.endBefore (
254+ (_data.first.value as Map <String , dynamic >? )? [widget.orderBy],
255+ );
238256 }
239257
240258 _liveStreamSub = latestDocQuery.onValue.listen (
0 commit comments