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 extract items from elements in a list to a new list in flutter

I have a list of Foo

List<Foo> fooList = [
  Foo("Zero", 0.0, 0.0],
  Foo("One", 1.0, 0.0],
  Foo("Two", 0.0, 1.0],
  Foo("Three", 1.0, 1.0],
];

class Foo {
  String name;
  double bar;
  double baz;

  Foo(this.name, this.bar, this.baz);
}

Is there an easy way to extract and make a list of only baz?

I’ve made it work with a for-loop

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

List<double> values = <double>[];
for(var item in fooList) {
  values.add(item.baz);
}

but I am hoping there are easier ways than that. Something like List<double>.from<fooList.baz>)

>Solution :

Yes you can simply do this by mapping.

final bazList = fooList.map((fooListItem) => fooListItem.baz)).toList();

bazList is a new array of baz items from the sequence of fooList

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