30
votes

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?

2
Based on the error I'd say mListUser is a List, not a Map. Is this the original, real code that you copied? - MarioP
i was using before a list and forgot to change the name, am defenetly using a map. As Gero as shown, i think im doing it right way, i guess there is something wrong in the object i use as a key, which by the way is the same Websocket i get from the onConnection on the Chathandler class (source here dartlang.org/docs/dart-up-and-running/contents/ch05.html) - user1964154
Well, without any further info, I can just stubbornly reiterate: The error message says that somewhere, you are trying to assign a String to a variable or parameter named "index", which has to be an integer. If this is the real code, this error may not even relate to the map at all. - MarioP

2 Answers

35
votes

Maybe it helps

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

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

11
votes

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