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
I think I understand why this "bug" happens but it was so baffling, my head did a couple of turns like in the exorcist when it happened. Consider this code:
One would expect the parameter P to be of type Map<String,dynamic>, since that's what it says right there, but sometimes P.runtimeType is, e.g. Map<String,List<String>> and then the line P["name"]=... results in a baffling run-time TypeError. By abusing Map<String,dynamic>.from() and friends, I was able to get around this but it seems like a crazy way to do things. It was even more bizarre in my case because, by adding one field to my data structure, whichever map was getting runtimeTyped to Map<String,List>, was instead correctly typed as Map<String,dynamic>, which was getting around the real problem here, which is that the compiler ignores my type declaration in the parameter list of the function.
Dart 2 generics are unsoundly covariant for historical reasons, with the consequence that errors like the above are only caught at runtime. There's a proposal to add sound variance to Dart. You can see some of the concrete proposals linked from here, and an article about the declaration site variance proposal here, written by my intern from last fall who implemented this in several of our tools.
Since this is (somewhat unfortunately) working as intended at the moment, I'm going to close this out in favor of the existing issues linked above.
I think I understand why this "bug" happens but it was so baffling, my head did a couple of turns like in the exorcist when it happened. Consider this code:
One would expect the parameter
P
to be of typeMap<String,dynamic>
, since that's what it says right there, but sometimesP.runtimeType
is, e.g.Map<String,List<String>>
and then the lineP["name"]=...
results in a baffling run-time TypeError. By abusingMap<String,dynamic>.from()
and friends, I was able to get around this but it seems like a crazy way to do things. It was even more bizarre in my case because, by adding one field to my data structure, whichever map was getting runtimeTyped to Map<String,List>, was instead correctly typed as Map<String,dynamic>, which was getting around the real problem here, which is that the compiler ignores my type declaration in the parameter list of the function.Here's a gist:
https://dartpad.dev/d041fe1d97bf1f6a378a45bc81cb4f28
The text was updated successfully, but these errors were encountered: