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
class Agent {
late int id;
late String name;
Agent.fromJson(Map<String, dynamic> json) {
id = json['id'] ?? 0;
name = json['name'] ?? '';
}
}
When the previous data type is dynamic, analyzer can't check that the type of 0 and 'name' match properly; json['name '] or 0 is finally assigned to name. Wouldn't it be better if json['name'] and 0 were verified with name respectively? json['name'] is dynamic, so it will not be verified, but 0 is not, then, the verification of name and 0 can not be ignored.
The text was updated successfully, but these errors were encountered:
cyjaysong
changed the title
if null expression, need enhanced type verification
if null expression, analyzer need enhanced type verification
May 23, 2022
To find the type of an expression of the form e1 ?? e2, the type of e1 is computed (let's call that T1) then the part of that type which allows null is eliminated, if possible (let's call that result T1n for "T1 non-nullable"), and then the upper bound with the type of e2 is taken (UP(T1n, T2)).
The type dynamic doesn't allow for removing null (Object contains the correct set of objects for this purpose, but it doesn't have the same behavior with this set of objects as dynamic does), so when T1 is dynamic then T1n is dynamic as well.
So id = json['id'] ?? e2 allows e2 to be an expression whose static type is almost any type (it can be String or int or MyClass<Whatever>, but it can't be void).
This means that we won't get the same typing precision as we would have had with id = e2. So it would be great if that situation could be improved. We have had proposals for improvements to this situation (and a very similar one involving conditional expressions, ?:), e.g., dart-lang/language#1618 (comment).
lrhn
added
the
area-language
Dart language related items (some items might be better tracked at github.com/dart-lang/language).
label
May 23, 2022
When the previous data type is
dynamic
, analyzer can't check that the type of 0 and 'name' match properly;json['name ']
or0
is finally assigned toname
. Wouldn't it be better ifjson['name']
and0
were verified withname
respectively?json['name']
isdynamic
, so it will not be verified, but0
is not, then, the verification ofname
and0
can not be ignored.The text was updated successfully, but these errors were encountered: