Why this code no raise an error?
List<String> x;
void main() {
x = [1,23,3,423,2];
print(x);
}
Sorry for the newbie question, but i just start learning dart, and i ask this because of my understanding that the x can only contain a list ofString and should raise an exception due to the value not a list of String but a list of num. This is a bug, or?
This is optional typing in action.
Whether you specify
List<String>orvar, your code will execute the same. The type annotations (List<String>) are used by the tools to verify your code.A good article to read on the dartlang site is one about optional types.
Edit: Actually, this is also an interesting point about Lists. If you initialize a list using a literal list( eg,
[]), then you are actually creating aList(), eg:If, however, you want a typed list using generics, you must use the constructor syntax, eg: