In the fromJSON initialiser in Flutter class, sometimes a double value will not exist. What is the best way to check for null, if not null, use toDouble()
This does not work because the toDouble is called on null
myValue = json['myValue'].toDouble() ?? null;
I really want short hand for
if (json['myValue'] != null) {
myValue = json['myValue'].toDouble();
}
Thanks
3
Answers
You can use try parse like
This instruction will do the job.
These operators provide a concise and efficient way to handle null values when working with JSON data in Flutter.
var name = jsonData[‘user’]?.[‘name’] ?? ‘Default Name’;