toJson method

  1. @override
Map<String, dynamic> toJson()
override

Returns a JSON version of the Nutriments.

When we want to erase a value, we need to set the value to ''. If we put null instead the server will interpret it as 0. If we don't set a value the server will keep the previous value.

Implementation

@override
Map<String, dynamic> toJson() {
  final Map<String, dynamic> result = <String, dynamic>{};
  for (final Nutrient nutrient in Nutrient.values) {
    for (final PerSize perSize in PerSize.values) {
      final String tag = _getTag(nutrient, perSize);
      final double? value = _map[tag];
      if (value != null) {
        result[tag] = value;
      } else if (_map.containsKey(tag)) {
        result[tag] = '';
      }
    }
  }
  return result;
}