dart language: Map<Object,String> how to add new pair?

心已入冬 提交于 2020-02-01 15:28:16

问题


Given:

Map<WebSocket,String> mListUser;
mListUser= new  Map<WebSocket,String>();

From what i understood now to add a new element i should just do:

mListUser[socket]="string";

instead im getting:

type 'String' is not a subtype of type 'int' of 'index'.

What am i doing wrong?


回答1:


Maybe it helps

final test = Map<String, int>();
test.putIfAbsent('58', () => 56);

if key doesn't exist, it will be putted into map.




回答2:


Maybe it helps.

Map<Object,String> map1= new Map<Object,String>();
Collections c = new Collections(); //some random class

map1[new Collections()]="arg1";
map1[c]="arg2";

map1.forEach((k,v)=>print("out: $k $v"));
print(map1[c]);

gets me this output:

out: Instance of 'Collections' arg2
out: Instance of 'Collections' arg1
arg2


来源:https://stackoverflow.com/questions/18175515/dart-language-mapobject-string-how-to-add-new-pair

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!