setDataSourceSearch method

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

Implementation

setDataSourceSearch({int? offset, bool refresh = false}) async {
  showSearch = true;
  try {
    initializeDataSourceAndConfirmData();
    loading = true;
    String text = removeDiacritics(filter.text.trim()).toLowerCase();
    (await actualDataSource!.getListSearch(text, quantityItensPage,
            offset ?? (page - 1) * quantityItensPage, selectModel,
            data: data,
            refresh: refresh,
            typeSearch: typeSearch,
            itemSort: itemSort))
        .listen((ResponseDataDataSource event) {
      error = null;

      /// Só altera se o texto ainda for idêntico ao pesquisado
      if (removeDiacritics(filter.text.trim()).toLowerCase() == text &&
          text == event.filter) {
        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!));
        total = event.total ?? 0;
        loading =
            !(removeDiacritics(filter.text.trim()).toLowerCase() == text);
        loaded = true;
      }
    }, 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;
  }
}