Open burekas7 opened 9 months ago
I see there is a manual solution like this: But how can I set this option for all my models using JsonSerializable, instead of doing it one by one?
import 'package:json_annotation/json_annotation.dart';
part 'my_data.g.dart';
@JsonSerializable()
class MyData {
@JsonKey(fromJson: _fromJson, toJson: _toJson)
Map<String, dynamic>? key;
MyData({this.key});
factory MyData.fromJson(Map<String, dynamic> json) => _$MyDataFromJson(json);
Map<String, dynamic> toJson() => _$MyDataToJson(this);
static Map<String, dynamic>? _fromJson(Map<String, dynamic>? json) {
// Check if json is an empty object and return null
return json != null && json.isEmpty ? null : json;
}
static Map<String, dynamic>? _toJson(Map<String, dynamic>? json) {
// Check if json is an empty object and return null
return json != null && json.isEmpty ? null : json;
}
}
Following to this issue: https://stackoverflow.com/questions/75522984/transforming-empty-nested-map-to-object-exception
I have the same problem. But this problem happens only when I run a unit tests. When I use the parse the same json in my app (Get the same json as dio response) it's ok, But when I use the same json as mock value for unit tests, it throws an exception since there is object value {}
What is the best way to handle cases of empty objects? Why does it happen only in the unit tests when I use the json object as mock value.
(When
e
value is{}
, not null)It comes from this: (When
json['locations']
value is[{}]
, not null) From the generated file. The problem is with the Location.fromJsonThe main problem is with
e as Map<String, dynamic>
that behaves differently.Thank you in advance.