read method

dynamic read(
  1. int pageNumber,
  2. int pageSize
)

Implementation

read(int pageNumber, int pageSize) async {
  isLoading = true;
  if (this.type == SourceType.local) {
    var models;
    var model = Dynamic.genericModelName(modelList);
    String modelName = model.toString();

    print('onPagePull :: Page Number => $pageNumber, SQL => ${this.sql}');
    Models modelListRead = new Models();
    var modelRF = modelListRead.get()[modelName] as ModelRF;
    models = await Db().get(modelRF.model!, this.sql, [], onLoad);
    if (models.length == 0) {
      currentPage--; //currentPage--;
    }
    if (this.onUpdateState != null)
      this.onUpdateState!(models, this, SourceType.local);
  } else if (this.type == SourceType.remote) {
    var model = Dynamic.genericModelName(modelList);
    String modelName = model.toString();

    dynamic fr = Api().fetchRequest;
    var _postWhere =
        whereFilter.replaceAll('\"%', "'%").replaceAll('%\"', "%'");
    print('DS _postWhere : $_postWhere');

    var _order = orderField == '' ? null : orderField + ' ' + order ?? 'asc';

    fr["UserId"] = await Libs.prefGet('UserId') ?? '0';
    fr["TableName"] = modelName;
    fr["PageNo"] = pageNumber + 1;
    fr["PageSize"] = pageSize;
    fr["TotalRecord"] = 100;
    fr["UserGuid"] = await Libs.prefGet('UserGuid') ?? '0';
    fr['ApiPacket'] = {
      "Packet": {
        "ModelName": modelName,
        //   "Select": this.select.isEmpty ? "SELECT * FROM $modelName" : this.select,
        "Select": this.select.isEmpty
            ? "SELECT * FROM \"$modelName\""
            : this.select,
        "Where": _postWhere,
        "OrderBy": _order ?? "",
        "IncludeChild": this.includeChild,
      },
      "PacketList": null
    };

    var baseUrl = Libs.config.http.apiBaseURL;
    var apiBaseURL = '$baseUrl/api/Fetch/GetPacket';
    var response = await rf.api.httpPost(
      url: apiBaseURL,
      requestBody: json.encode(fr),
    );
    print('DS _postResponse : ${response.body}');

    var models = jsonDecode(response.body)['ApiPacket']['PacketList'];
    _httpTotalCount = jsonDecode(response.body)['TotalRecord'];

    //await Future.delayed(Duration(microseconds: 150)); //FOR TEST
    if (this.onUpdateState != null)
      this.onUpdateState!(models, this, SourceType.remote);
    if (onLoad != null) onLoad!(models);
  } else if (this.type == SourceType.memory) {
    if (this.onUpdateState != null)
      this.onUpdateState!(modelList, this, SourceType.memory);
  }
}