Skip to content

Commit

Permalink
feat: Adds includeMetadataChanges and options
Browse files Browse the repository at this point in the history
  • Loading branch information
vedartm committed Nov 17, 2021
1 parent 3cfab82 commit 69c1f3d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 12 deletions.
6 changes: 3 additions & 3 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ class HomePage extends StatelessWidget {
// item builder type is compulsory.
itemBuilderType:
PaginateBuilderType.listView, //Change types accordingly
itemBuilder: (index, context, documentSnapshot) {
final data = documentSnapshot.data() as Map?;
itemBuilder: (context, documentSnapshots, index) {
final data = documentSnapshots[index].data() as Map?;
return ListTile(
leading: const CircleAvatar(child: Icon(Icons.person)),
title:
data == null ? const Text('Error in data') : Text(data['name']),
subtitle: Text(documentSnapshot.id),
subtitle: Text(documentSnapshots[index].id),
);
},
// orderBy is compulsory to enable pagination
Expand Down
16 changes: 12 additions & 4 deletions lib/bloc/pagination_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@ class PaginationCubit extends Cubit<PaginationState> {
this._limit,
this._startAfterDocument, {
this.isLive = false,
this.includeMetadataChanges = false,
this.options,
}) : super(PaginationInitial());

DocumentSnapshot? _lastDocument;
final int _limit;
final Query _query;
final DocumentSnapshot? _startAfterDocument;
final bool isLive;
final bool includeMetadataChanges;
final GetOptions? options;

final _streams = <StreamSubscription<QuerySnapshot>>[];

Expand All @@ -46,13 +50,15 @@ class PaginationCubit extends Cubit<PaginationState> {
_lastDocument = null;
final localQuery = _getQuery();
if (isLive) {
final listener = localQuery.snapshots().listen((querySnapshot) {
final listener = localQuery
.snapshots(includeMetadataChanges: includeMetadataChanges)
.listen((querySnapshot) {
_emitPaginatedState(querySnapshot.docs);
});

_streams.add(listener);
} else {
final querySnapshot = await localQuery.get();
final querySnapshot = await localQuery.get(options);
_emitPaginatedState(querySnapshot.docs);
}
}
Expand All @@ -69,7 +75,7 @@ class PaginationCubit extends Cubit<PaginationState> {
} else if (state is PaginationLoaded) {
final loadedState = state as PaginationLoaded;
if (loadedState.hasReachedEnd) return;
final querySnapshot = await localQuery.get();
final querySnapshot = await localQuery.get(options);
_emitPaginatedState(
querySnapshot.docs,
previousList:
Expand All @@ -90,7 +96,9 @@ class PaginationCubit extends Cubit<PaginationState> {
} else if (state is PaginationLoaded) {
final loadedState = state as PaginationLoaded;
if (loadedState.hasReachedEnd) return;
final listener = localQuery.snapshots().listen((querySnapshot) {
final listener = localQuery
.snapshots(includeMetadataChanges: includeMetadataChanges)
.listen((querySnapshot) {
_emitPaginatedState(
querySnapshot.docs,
previousList:
Expand Down
27 changes: 22 additions & 5 deletions lib/paginate_firestore.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ class PaginateFirestore extends StatefulWidget {
this.header,
this.footer,
this.isLive = false,
this.includeMetadataChanges = false,
this.options,
}) : super(key: key);

final Widget bottomLoader;
Expand All @@ -69,12 +71,18 @@ class PaginateFirestore extends StatefulWidget {
final Widget? header;
final Widget? footer;

/// Use this only if `isLive = false`
final GetOptions? options;

/// Use this only if `isLive = true`
final bool includeMetadataChanges;

@override
_PaginateFirestoreState createState() => _PaginateFirestoreState();

final Widget Function(Exception)? onError;

final Widget Function(int, BuildContext, DocumentSnapshot) itemBuilder;
final Widget Function(BuildContext, List<DocumentSnapshot>, int) itemBuilder;

final void Function(PaginationLoaded)? onReachedEnd;

Expand Down Expand Up @@ -187,7 +195,10 @@ class _PaginateFirestoreState extends State<PaginateFirestore> {
return widget.bottomLoader;
}
return widget.itemBuilder(
index, context, loadedState.documentSnapshots[index]);
context,
loadedState.documentSnapshots,
index,
);
},
childCount: loadedState.hasReachedEnd
? loadedState.documentSnapshots.length
Expand Down Expand Up @@ -233,8 +244,11 @@ class _PaginateFirestoreState extends State<PaginateFirestore> {
_cubit!.fetchPaginatedList();
return widget.bottomLoader;
}
return widget.itemBuilder(itemIndex, context,
loadedState.documentSnapshots[itemIndex]);
return widget.itemBuilder(
context,
loadedState.documentSnapshots,
itemIndex,
);
}
return widget.separator;
},
Expand Down Expand Up @@ -290,7 +304,10 @@ class _PaginateFirestoreState extends State<PaginateFirestore> {
return widget.bottomLoader;
}
return widget.itemBuilder(
index, context, loadedState.documentSnapshots[index]);
context,
loadedState.documentSnapshots,
index,
);
},
childCount: loadedState.hasReachedEnd
? loadedState.documentSnapshots.length
Expand Down

0 comments on commit 69c1f3d

Please sign in to comment.