我有这门课
@freezed
abstract class CartEntity with _$CartEntity {
const factory CartEntity.empty(String status, String message) = _Empty;
const factory CartEntity.notEmpty(int x) = _NotEmpty;
factory CartEntity.fromJson(Map<String, dynamic> json) =>
_$CartEntityFromJson(json);
}
而这个转换器
class CartEntityConverter
implements JsonConverter<CartEntity, Map<String, dynamic>> {
const CartEntityConverter();
@override
CartEntity fromJson(Map<String, dynamic> json) {
//the problem here
print(json);// null
return _Empty.fromJson(json);
}
@override
Map<String, dynamic> toJson(CartEntity object) {
return object.toJson();
}
}
而这个包装类
@freezed
abstract class CartEntityWrapper with _$CartEntityWrapper {
const factory CartEntityWrapper(@CartEntityConverter() CartEntity cartEntity) =
CartEntityWrapperData;
factory CartEntityWrapper.fromJson(Map<String, dynamic> json) =>
_$CartEntityWrapperFromJson(json);
}
我叫
final cartEntity = CartEntityWrapperData.fromJson({'x':'y'});
print(cartEntity);
在 CartEntityConverter 中的 fromJson 方法总是接收空 json 那么我做错了什么?