Search code examples
androidjsonflutterasp.net-web-api2

Flutter: How to use json sort to get latest date


I want integrate Flutter with an ASP.NET API. the problem is when I to try sortjson from api which can get the latest date from datelastinsp or atleast sort from new date to older. below is the code

class ApiProvider {
  final Dio _dio = Dio();
  final String _url = 'https://thisisapi.com/c&s/drainagepi/all';
  

  Future<listmodel> fetchlistList() async {
    try {
      Response response = await _dio.get(_url);
      
       List<listmodel> sortList = response.data.map((d) => listmodel.fromJson(d)).toList();

       sortList.sort((a, b) {
             DateTime dateA = DateTime.parse(a.rows![a].dateofinsp);
             DateTime dateB = DateTime.parse(b as String);

      return a.rows.elementAt(index)
    });
       
      return listmodel.fromJson(response.data);
    } catch (error, stacktrace) {
      print("Exception occured: $error stackTrace: $stacktrace");
      return listmodel.withError("Data not found / Connection issue");
    }
  }
}

there's an error on sortlist and this is my model, i want to compare the date between list so, only row per drainage id

class listmodel {
  String? drainageid;
  int? datacount;
  int? resultcount;
  String? page;
  List<Rows>? rows;
  String? error;

  listmodel(
      {this.drainageid,
      this.datacount,
      this.resultcount,
      this.page,
      this.rows});

  listmodel.withError(String errorMessage) {
    error = errorMessage;
  }

  listmodel.fromJson(Map<String, dynamic> json) {
    drainageid = json['drainageid'];
    datacount = json['datacount'];
    resultcount = json['resultcount'];
    page = json['page'];
    if (json['rows'] != null) {
      rows = <Rows>[];
      json['rows'].forEach((v) {
        rows!.add(Rows.fromJson(v));
      });
    }
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['drainageid'] = drainageid;
    data['datacount'] = datacount;
    data['resultcount'] = resultcount;
    data['page'] = page;
    if (rows != null) {
      data['rows'] = rows!.map((v) => v.toJson()).toList();
    }
    return data;
  }
}

class Rows {
  String? id;
  DateTime? dateoflastinsp;
  DateTime? dateofinsp;
  String? inspby;

  Rows({this.id, this.dateoflastinsp, this.dateofinsp, this.inspby});

  Rows.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    dateoflastinsp = json['dateoflastinsp'];
    dateofinsp = json['dateofinsp'];
    inspby = json['inspby'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['id'] = id;
    data['dateoflastinsp'] = dateoflastinsp;
    data['dateofinsp'] = dateofinsp;
    data['inspby'] = inspby;
    return data;
  }
}

this is small result of the json

{
  "datacount": 98488,
  "drainageid": "",
  "page": "",
  "resultcount": 98488,
  "rows": [
    {
      "dateofinsp": "2001-10-10T00:00:00",
      "dateoflastinsp": null,
      "id": "DR/C1/IC/H/-/GPG/PU/-/RCP/01",
      "inspby": "SAW/ZAG"
    },
    {
      "dateofinsp": "2002-09-24T00:00:00",
      "dateoflastinsp": "2001-10-10T00:00:00",
      "id": "DR/C1/IC/H/-/GPG/PU/-/RCP/01",
      "inspby": "SAW+NBN"
    },
    {
      "dateofinsp": "2003-09-18T00:00:00",
      "dateoflastinsp": "2002-09-24T00:00:00",
      "id": "DR/C1/IC/H/-/GPG/PU/-/RCP/01",
      "inspby": "SAW/NBN"
    },
    {
      "dateofinsp": "2004-09-08T00:00:00",
      "dateoflastinsp": "2003-09-18T00:00:00",
      "id": "DR/C1/IC/H/-/GPG/PU/-/RCP/01",
      "inspby": "SAW/NBN"
    },
    {
      "dateofinsp": "2005-05-05T00:00:00",
      "dateoflastinsp": "2004-09-08T00:00:00",
      "id": "DR/C1/IC/H/-/GPG/PU/-/RCP/01",
      "inspby": "MZB/ASMG"
    },
    {
      "dateofinsp": "2006-05-04T00:00:00",
      "dateoflastinsp": "2005-05-05T00:00:00",
      "id": "DR/C1/IC/H/-/GPG/PU/-/RCP/01",
      "inspby": "MZB/ASZ"
    },
    {
      "dateofinsp": "2007-05-09T00:00:00",
      "dateoflastinsp": "2006-05-04T00:00:00",
      "id": "DR/C1/IC/H/-/GPG/PU/-/RCP/01",
      "inspby": "Ahmad/Haizul"
    },
    {
      "dateofinsp": "2008-05-07T00:00:00",
      "dateoflastinsp": "2007-05-09T00:00:00",
      "id": "DR/C1/IC/H/-/GPG/PU/-/RCP/01",
      "inspby": "Harzazi/Zulkornain"
    },
    {
      "dateofinsp": "2009-05-27T00:00:00",
      "dateoflastinsp": "2008-05-07T00:00:00",
      "id": "DR/C1/IC/H/-/GPG/PU/-/RCP/01",
      "inspby": "Harzazi/Sahril"
    },
    {
      "dateofinsp": "2010-05-19T00:00:00",
      "dateoflastinsp": "2009-05-27T00:00:00",
      "id": "DR/C1/IC/H/-/GPG/PU/-/RCP/01",
      "inspby": "Harzazi/Fairus"
    },
    {
      "dateofinsp": "2011-06-02T00:00:00",
      "dateoflastinsp": "2010-05-19T00:00:00",
      "id": "DR/C1/IC/H/-/GPG/PU/-/RCP/01",
      "inspby": "Harzazi/Fairus"
    },
    {
      "dateofinsp": "2012-06-21T00:00:00",
      "dateoflastinsp": "2011-06-02T00:00:00",
      "id": "DR/C1/IC/H/-/GPG/PU/-/RCP/01",
      "inspby": "HHH/MFS"
    },
    {
      "dateofinsp": "2013-06-20T00:00:00",
      "dateoflastinsp": "2012-06-21T00:00:00",
      "id": "DR/C1/IC/H/-/GPG/PU/-/RCP/01",
      "inspby": "M.Furus/N.M.Ariff"
    },
    {
      "dateofinsp": "2014-08-19T00:00:00",
      "dateoflastinsp": "2013-06-20T00:00:00",
      "id": "DR/C1/IC/H/-/GPG/PU/-/RCP/01",
      "inspby": "M.FAIRUS+NIK ARIFF"
    },
    {
      "dateofinsp": "2015-10-20T00:00:00",
      "dateoflastinsp": "2014-08-19T00:00:00",
      "id": "DR/C1/IC/H/-/GPG/PU/-/RCP/01",
      "inspby": "Adib/Shafina"
    },
    {
      "dateofinsp": "2016-11-07T00:00:00",
      "dateoflastinsp": "2015-10-20T00:00:00",
      "id": "DR/C1/IC/H/-/GPG/PU/-/RCP/01",
      "inspby": "Athirah+Am+Khairi"
    },
    {
      "dateofinsp": "2017-09-28T00:00:00",
      "dateoflastinsp": "2016-11-07T00:00:00",
      "id": "DR/C1/IC/H/-/GPG/PU/-/RCP/01",
      "inspby": "Am+Athirah+Asyraf"
    },
    {
      "dateofinsp": "2018-06-06T00:00:00",
      "dateoflastinsp": "2017-09-28T00:00:00",
      "id": "DR/C1/IC/H/-/GPG/PU/-/RCP/01",
      "inspby": "Am+Amirul"
    },
    {
      "dateofinsp": "2019-10-02T00:00:00",
      "dateoflastinsp": "2018-06-06T00:00:00",
      "id": "DR/C1/IC/H/-/GPG/PU/-/RCP/01",
      "inspby": "Am/Amirul"
    },
    {
      "dateofinsp": "2020-10-07T00:00:00",
      "dateoflastinsp": "2019-10-02T00:00:00",
      "id": "DR/C1/IC/H/-/GPG/PU/-/RCP/01",
      "inspby": "Am/Amirul"
    },
    {
      "dateofinsp": "2022-10-05T00:00:00",
      "dateoflastinsp": "2020-10-07T00:00:00",
      "id": "DR/C1/IC/H/-/GPG/PU/-/RCP/01",
      "inspby": "Am/Amirul"
    },
    {
      "dateofinsp": "2001-10-10T00:00:00",
      "dateoflastinsp": null,
      "id": "DR/C1/IC/H/-/GPG/PU/-/RCP/02",
      "inspby": "SAW/ZAG"
    }
  ]
}

among those, i only want latest dateofinsp one from of list row for per id, how to do that?


Solution

  • I think you have issue at RowModel

    class Rows {
      String? id;
      DateTime? dateoflastinsp;
      DateTime? dateofinsp;
      String? inspby;
    
      Rows({this.id, this.dateoflastinsp, this.dateofinsp, this.inspby});
    
      Rows.fromJson(Map<String, dynamic> json) {
        id = json['id'];
        dateoflastinsp = json['dateoflastinsp'];
        dateofinsp = json['dateofinsp'];
        inspby = json['inspby'];
      }
    
      Map<String, dynamic> toJson() {
        final Map<String, dynamic> data = <String, dynamic>{};
        data['id'] = id;
        data['dateoflastinsp'] = dateoflastinsp;
        data['dateofinsp'] = dateofinsp;
        data['inspby'] = inspby;
        return data;
      }
    }
    

    Issue is at this line:

        dateoflastinsp = json['dateoflastinsp'];
        dateofinsp = json['dateofinsp'];
    

    it should be :

        dateoflastinsp = json['dateoflastinsp'] == null
              ? null
              : DateTime.parse(json['dateoflastinsp'] as String),
        ....