In Javascript, to conditionally add a value to an Object, I can do something like this:
const obj = {
...(myCondition && {someKey: "someValue"})
}
Can I do something similar to pass in a named parameter in Dart / Flutter? For example, if I have the below code, is there a way to conditionally pass the place parameter only if it exists in the json passed into the fromJson factory function.
factory SearchResult.fromJson(Map<String, dynamic> json) {
return SearchResult(
id: json['id'],
displayString: json['displayString'],
name: json['name'],
recordType: json['recordType'],
collection: json['collection'],
place: GeoJson.fromJson(json['place']),
properties: json['properties']
);
}
null, theGeoJson.fromJsonmethod throws an error because it expects a value. Is there a way to allow a value to be aStringornullkind of like in Typescript you can sayprivate someAttribute: string | null.