Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #33 #34

Merged
merged 7 commits into from
Dec 27, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## 3.0.1

- fix: docs not loading after the first sroll
- fix: docs not loading after the first scroll

## 3.0.0

Expand Down
18 changes: 15 additions & 3 deletions lib/src/realtime_db_pagination.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class RealtimeDBPagination extends StatefulWidget {
required this.itemBuilder,
required this.orderBy,
super.key,
this.descending = false,
this.separatorBuilder,
this.limit = 10,
this.viewType = ViewType.list,
Expand Down Expand Up @@ -90,6 +91,11 @@ class RealtimeDBPagination extends StatefulWidget {
/// If null, the data will be sorted by the key.
final String? orderBy;

/// Fetches and shows the last data first from the database.
///
/// Default value is `false`.
final bool descending;

/// The builder to use to render the separator.
///
/// Only used if [viewType] is [ViewType.list].
Expand Down Expand Up @@ -203,7 +209,9 @@ class _RealtimeDBPaginationState extends State<RealtimeDBPagination> {
if (getMore) setState(() => _isFetching = true);

final docsLimit = _data.length + (getMore ? widget.limit : 0);
var docsQuery = widget.query.limitToFirst(docsLimit);
var docsQuery = widget.descending
? widget.query.limitToLast(docsLimit)
: widget.query.limitToFirst(docsLimit);
if (_data.isNotEmpty) {
docsQuery = docsQuery.startAt(
Map<String, dynamic>.from(
OutdatedGuy marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -260,7 +268,9 @@ class _RealtimeDBPaginationState extends State<RealtimeDBPagination> {
// To cancel previous live listener when new one is set.
final tempSub = _liveStreamSub;

var latestDocQuery = widget.query.limitToFirst(1);
var latestDocQuery = widget.descending
? widget.query.limitToLast(1)
: widget.query.limitToFirst(1);
if (_data.isNotEmpty) {
latestDocQuery = latestDocQuery.endBefore(
Map<String, dynamic>.from(
OutdatedGuy marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -319,7 +329,9 @@ class _RealtimeDBPaginationState extends State<RealtimeDBPagination> {
: _data.isEmpty
? widget.onEmpty
: BuildPagination(
items: _data,
items: widget.descending
? _data.reversed.toList()
: _data,
itemBuilder: widget.itemBuilder,
separatorBuilder: widget.separatorBuilder ?? separatorBuilder,
isLoading: _isFetching,
Expand Down