I am trying to double-check if the User object is successfully created, but Null saftey says
the operand cannot be null, so the condition is always true
What if in a scenario where the json data contains invalid type, in this case there might be some errors when creating the user object
class User {
String? name;
String? age;
User({name, age}) {
this.name = name;
this.age = age;
}
factory User.fromJson(dynamic json) {
return User(name: json['name'], age: json['age']);
}
}
void main() {
String data = '{name: "mike",age: "2"}';
User user = User.fromJson(data);
if (user != null) { // Warning: "The operand can't be null, so the condition is always true. Remove the condition."
}
}
Please advise, Thank you! :)