setDataSource method

void setDataSource({
  1. int? offset,
  2. bool refresh = false,
})
inherited

Implementation

void setDataSource({int? offset, bool refresh = false}) async {
  try {
    initializeDataSource();
    GroupFilterExp groupFilterExp = buildFilterExpression();
    showSearch = groupFilterExp.filterExps.isEmpty;
    loading = true;
    offset ??= (page - 1) * quantityItensPage;
    (await actualDataSource!.getList(quantityItensPage, offset, selectModel,
            data: data,
            refresh: refresh,
            itemSort: itemSort,
            filter: groupFilterExp))
        .listen((event) {
      error = null;
      if (filter.text.trim().isEmpty) {
        list.clear();

        /// Não aplica no debug/profile para captura de erros
        if (UtilsPlatform.isRelease) {
          event.data = event.data.distinctBy((e) => e.id);
        }
        event.data.forEach((item) {
          bool present = selectedList.any((element) => element.id == item.id);
          if (item.isSelected == true) {
            if (!present) {
              /// Caso o item esteja selecionado e não esteja na lista selectedList
              selectedList.add(item);
            }
          } else {
            item.isSelected = present;
          }
          list.add(item);
        });
        list = ObservableList.of(list.sortedBy((e) => e.position!));
        loading = false;
        loadingMore = false;
        loaded = true;
        total = event.total ?? 0;
        setDataType();
      }
    }, onError: (error) {
      print(error);
      loading = false;
      loadingMore = false;
      this.error = error;
    });
  } catch (error, stackTrace) {
    UtilsSentry.reportError(error, stackTrace);
    print(error);
    loading = false;
    loadingMore = false;
    this.error = error;
  }
}