Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 33 additions & 12 deletions packages/firebase_ui_firestore/lib/src/query_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -178,18 +178,43 @@ class _FirestoreQueryBuilderState<Document>

final query = widget.query.limit(expectedDocsCount);

// A new listener first emits whatever the local cache holds, which is
// often less than the pages already shown. Rendering that would shrink
// the list and reset the scroll position, so keep the current docs until
// the server responds. A drop of one doc is still rendered, since that is
// what a local delete looks like. Metadata changes are included so that a
// server result identical to the cache still arrives as an event.
var awaitingServer = nextPage;
var hasRendered = false;

_querySubscription = query
.snapshots(includeMetadataChanges: widget.includeMetadataChanges)
.snapshots(
includeMetadataChanges:
widget.includeMetadataChanges || awaitingServer,
)
.listen(
(event) {
setState(() {
if (nextPage) {
_snapshot = _snapshot.copyWith(isFetchingMore: false);
} else {
_snapshot = _snapshot.copyWith(isFetching: false);
if (awaitingServer) {
if (event.metadata.isFromCache &&
event.size < _snapshot.docs.length - 1) {
return;
}
awaitingServer = false;
}

// Metadata-only events have no doc changes. Only render them if
// the consumer asked for metadata changes.
if (hasRendered &&
!widget.includeMetadataChanges &&
event.docChanges.isEmpty) {
return;
}
hasRendered = true;

setState(() {
_snapshot = _snapshot.copyWith(
isFetching: false,
isFetchingMore: false,
hasData: true,
docs: event.size < expectedDocsCount
? event.docs
Expand All @@ -203,13 +228,9 @@ class _FirestoreQueryBuilderState<Document>
},
onError: (Object error, StackTrace stackTrace) {
setState(() {
if (nextPage) {
_snapshot = _snapshot.copyWith(isFetchingMore: false);
} else {
_snapshot = _snapshot.copyWith(isFetching: false);
}

_snapshot = _snapshot.copyWith(
isFetching: false,
isFetchingMore: false,
error: error,
stackTrace: stackTrace,
hasError: true,
Expand Down
Loading
Loading