Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to fix mapIndexed function in flutter?

I have a function that map from one list and add every item to another list:

List<TimeSeriesValues> list = (data["list"] as List).mapIndexed(
  (index, e) => TimeSeriesValues(DateTime.tryParse(e["detectedAt"])!,int.parse(e['score']))).toList();

This is TimeSeriesValues class:

class TimeSeriesValues {
  final DateTime time;
  final int values;

  TimeSeriesValues(this.time, this.values);
}

And I want to add if statements, like if e[‘score’] != 0 then we add the item to the list otherwise not.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

If e[‘score’] != 0 then add TimeSeriesValues(DateTime.tryParse(e["detectedAt"])!, int.parse(e[‘score’])) to list else not

This is the response from Api:

[{detectedAt: 2022-11-28T00:00:00.000Z, score: 57},...] // data['list']

>Solution :

Try this:

List<TimeSeriesValues> list = (data["list"] as List).mapIndexed(
  (index, e) => TimeSeriesValues(DateTime.tryParse(e["detectedAt"])!,int.parse(e['score']))).toList();
List<TimeSeriesValues> filteredList = list.where((e) => e.values != 0).toList();
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading