Felix Wang
Felix Wang

Reputation: 157

Method map() dose not work for a List in a Map

I got a Map like this:

Map userInfo={
     'name':'jack',
     'age':30,
     'interests':[
       {'name':'swimming','level':1},{'name':'running','level':2},
     ]
    }

and now I wanna use method map() to convert interests to a list of Widgets, if I use userInfo['interests'] below, it will not work:

userInfo['interests'].map((item){return new Text(item['name'])}).toList() //not work

but, if save userInfo['interests'] in a variable first, it works:

List tmpVariable=userInfo['inetrests'];
tmpVariable.map((item){return new Text(item['name'])}).toList()  //this works

so the question is that if I got a structure like above, can I map it directly? Or what's best way to deal with that? Use variable is not so good I think. :)

Upvotes: 0

Views: 39

Answers (1)

diegoveloper
diegoveloper

Reputation: 103421

Try this:

(userInfo['interests'] as List).map((item){return new Text(item['name'])}).toList() 

Upvotes: 1

Related Questions