1

I have this List of objects

const List categories = [
  {'title': 'category 1', 'value': 1000},
  {'title': 'category 2', 'value': 2000},
  {'title': 'category 3', 'value': 3000},
];

And I'm trying to map this list to render some UI in a Column widget, so I did this

Column(
  mainAxisAlignment: MainAxisAlignment.start,
  crossAxisAlignment: CrossAxisAlignment.start,
  children: categories.map((e) => Category(title: e.title, value: e.value)).toList(),
),

But it gives me this error enter image description here

1 Answer 1

3

Here, I guess you came from javascript, well dart is different a little bit, the elements inside the List are called maps, with Map type, to get a value from that map you need to get it like this :

map["key"]

the keys should be String, so in your case, this will work fine :

`Column(
  mainAxisAlignment: MainAxisAlignment.start,
  crossAxisAlignment: CrossAxisAlignment.start,
  children: categories.map((e) => Category(title: e["title"], value: 
  e["value"])).toList(),
),
Sign up to request clarification or add additional context in comments.

2 Comments

Is it that obvious? XD, But anyways it works as it should be. Thank You
no problem mate, selecting this as the right answer could help someone else

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.