addIfNotNull method
Adds value
to map using key
if and only if value
is not null.
If value
is not null but is not an Object, it is added to the map as a
dynamic value. This is useful for adding primitive types like int, double,
String, etc. to the map.
This is a convenience method that is equivalent to:
if (value != null) {
map[key] = value;
}
but is more concise and easier to read.
Implementation
Map<String, dynamic> addIfNotNull(String key, dynamic value) {
if (value != null && value is Object) {
putIfAbsent(key, () => value);
} else if (value != null) {
putIfAbsent(key, () => value as dynamic);
}
return this;
}