30

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?

3
  • Based on the error I'd say mListUser is a List, not a Map. Is this the original, real code that you copied? Commented Aug 12, 2013 at 7:09
  • 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) Commented Aug 12, 2013 at 9:55
  • 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. Commented Aug 12, 2013 at 10:17

2 Answers 2

36

Maybe it helps

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

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

Sign up to request clarification or add additional context in comments.

1 Comment

I am using a `Map<DateTime, Lis<Event>>' (Event is a random class). Every time I add a new pair, the key (DateTime) is correctly added, but the list is replaced in all keys. Any help? Any Ideas?
11

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

1 Comment

I found the problem or identify it. Since im using 'dart:io'; instead of import 'dart:html'; is seems that in the first, WebSocket is an abstract class and trying to use it as a key just doen't work, at least in that way. Note that in a project with 'dart:html'; everything works as expected as suggested above by Gero

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.