Home > OS >  filtering data from one date to another in flutter. The data is structured as below
filtering data from one date to another in flutter. The data is structured as below

Time:06-20

[{date: 21/6/2022, amount: 200, allocation: allocated, month: June, year: 2022, paymentcode: INVGT899, phone: 254704122812, docid: LJGPV5Lx2shyRmZsdf8L, name: Adam Jiwawa, userid: yuWufPcMu9VU6VTX2xrx33XOA323, paymentref: PAY77293}, {date: 20/6/2022, amount: 3000, allocation: allocated, month: June, paymentcode: PAY2002, phone: 254704122812, year: 2022, docid: TM6yPolBTKJNHA4AzTpg, name: Frank Lyn, userid: yuWufPcMu9VU6VTX2xrx33XOA323, paymentref: INV9000}, {date: 12/10/2022, amount: 20000, allocation: allocated, phone: 254704122812, paymentcode: INCV&88s, docid: joh64rHBqrJbV5WQX4lg, name: Ruth Wagoa, userid: 38G6mrsvzVNsdyHGDpmlFsDQz733, paymentref: PDY777888}, {date: 12/12/2023, allocation: unallocated, amount: 2000, phone: 254704122812, paymentcode: 203030cD, docid: kC7MEgRvnQhfwktAo2dI, name: James Ruponzo, userid: 38G6mrsvzVNsdyHGDpmlFsDQz733, paymentref: PAy6272},]

CodePudding user response:

This is in the JSON format to access it you have to use its key. for example, if you want to access the amount value of the first item. First, as you can see it is in the List and want to access the first item to access it we have to use the list[0] and it is a map to access an item in the map we have to use the key i.e for the amount the key is "amount" so to access the amount of the first item the code will be

list[0]["amount"]

create a list for the items e.g inRageItems and access date of every product and compare it with the lower boundary of the range and with the upper boundary of the range and add that item to the list if it is in the range. To compare date flutter provide a method which is itemDate.compareTo(rangeBoundary) it will return a positive value if itemDate is after rangeBoundary Date and vice versa. create a list for the items e.g inRageItems and access date of every product and compare it with the lower boundary of the range and with the upper boundary of the range and add that item to the list if it is in the range. To compare date flutter provide a method which is itemDate.compareTo(rangeBoundary) it will return a positive value if itemDate is after rangeBoundary Date and vice versa. another method is itemDate.isAfter(lowerBoundary) and itemDate.isBefore(upperBoundary)

CodePudding user response:

let your list is

List<Map<String, dynamic>> data = [
{
  "date": "21/6/2022",
  "amount": 200,
  "allocation": true,
},
{
  "date": "20/6/2022",
  "amount": 3000,
  "allocation": true,
},
{
  "date": "12/10/2022",
  "amount": 20000,
  "allocation": true,
},
{
  "date": "12/12/2023",
  "allocation": false,
  "amount": 2000,
},
];
DateTime lowerBoundary = DateTime(2022, 6, 21);
DateTime upperBoundary = DateTime(2022, 6, 29);
var filteredData = [];
data.forEach((element) {
var dateString = element["date"];
dateString = dateString.split("/");
DateTime formattedDate = DateTime(
  int.parse(dateString[2]),
  int.parse(dateString[1]),
  int.parse(dateString[0]),
);
if (formattedDate.isAfter(lowerBoundary) &&
    formattedDate.isBefore(upperBoundary)) {
  filteredData.add(element);
}
});

if you have any problem with this then let me know I will try to answer you. Maybe there will be an easy method but using this you can achieve your desired functionality.

  • Related