load method

Future<int?> load({
  1. sortChoice? sortBy,
  2. List<FilterFunc> filters = const [],
  3. bool loadMore = false,
})

Implementation

Future<int?> load({sortChoice? sortBy,
  List<FilterFunc>filters=const [], bool loadMore=false})async{
  if(myid==null)return null;
  sortBy=sortBy??config.initialSortBy;
  loadMore=loadMore&&state.value!=null;

  var qr=fbCol.orderBy(sortBy?.key??FieldPath.documentId,
      descending: sortBy?.descend??false).limit(config.pageSize);
  filters.forEach((filter) {qr=filter(qr); });
  if(config.constFilters!=null)config.constFilters!(myid!).forEach((filter) {qr=filter(qr); });

  if(loadMore&&state.value!.isNotEmpty) {
    qr=qr.startAfter([config.toDocWithId(state.value!.last)[sortBy?.key??idKey]]);
  }
  QuerySnapshot<Map<String,dynamic>> r;
  List<T> added;
  if(config.onLoad!=null)config.onLoad!(true);
  try{
    r=await qr.get();
    ///handel FutureOr for FromDoc
    added=await Future.wait<T>(r.docs.map((e) =>
        Future(()=>config.fromDoc(e))));
    if(config.onLoad!=null)config.onLoad!(false);
  }on FirebaseException catch(e){
    config.onException(e);
    if(config.onLoad!=null)config.onLoad!(false);
    return null;
  }

  if(mounted) state=AsyncValue<List<T>>.data(loadMore?List.of(addMore(state.value,added)):added);
  if(r.size<config.pageSize&&config.onReachEnd!=null) {
    config.onReachEnd!(state.value!.length);
  }
  return added.length;
}