You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
At compile time, Is there any way to make dart ensure that the type of key that I'm passing is the correct type for that Map?
As one can imagine, this kind of mistake is very easy to make, and also easily preventable at compile time. (We just encountered a hard to track bug in our app because of it!)
There is no way to avoid an incompatible type being allowed, while also allowing a Map<String, String> to be used as a Map<Object, Object>. Dart has covariant generics, so the string map is assignable to the object map type. By allowing any value to be used as key in lookup (and a few other methods), you can use the map at the supertype, as long as you only use it for reading. Writing will still necessarily throw.
The following code compiles successfully, but in my mind, it should have not -
Output -
At compile time, Is there any way to make dart ensure that the type of key that I'm passing is the correct type for that Map?
As one can imagine, this kind of mistake is very easy to make, and also easily preventable at compile time. (We just encountered a hard to track bug in our app because of it!)
dartpad
The text was updated successfully, but these errors were encountered: