Open
Description
Breaking Change (1):
For private classes, the name of the FromJson changed
Take this class for example:
@freezed
class _SwapAssetDto with _$_SwapAssetDto {
const _SwapAssetDto._();
const factory _SwapAssetDto({
@JsonKey(name: "unit") required String unit,
@JsonKey(name: "name") required String name,
// ignore: unused_element
@JsonKey(name: "decimals") int? decimals,
// ignore: unused_element
@JsonKey(name: "ticker") String? ticker,
}) = __SwapAssetDto;
// 2.4.3
factory _SwapAssetDto.fromJson(Map<String, dynamic> json) => _$SwapAssetDtoFromJson(json);
// 2.4.2 and below
//factory _SwapAssetDto.fromJson(Map<String, dynamic> json) => _$_SwapAssetDtoFromJson(json);
}
The underscore after the $
is now missing.
`
Breaking Change (2):
Cannot generate toJson anymore. This used to work with 2.4.2.
P.s. If you run the code below with 2.4.2, you'll need to fix the fromJson methods (mentioned in first issue)
// ignore_for_file: invalid_annotation_target
import 'package:freezed_annotation/freezed_annotation.dart';
part 'swap_assets.freezed.dart';
part 'swap_assets.g.dart';
@freezed
class _SwapAssetDto with _$_SwapAssetDto {
const _SwapAssetDto._();
const factory _SwapAssetDto({
@JsonKey(name: "unit") required String unit,
@JsonKey(name: "name") required String name,
// ignore: unused_element
@JsonKey(name: "decimals") int? decimals,
// ignore: unused_element
@JsonKey(name: "ticker") String? ticker,
}) = __SwapAssetDto;
factory _SwapAssetDto.fromJson(Map<String, dynamic> json) => _$SwapAssetDtoFromJson(json);
}
@freezed
class _SwapAssetsDto with _$_SwapAssetsDto {
const _SwapAssetsDto._();
const factory _SwapAssetsDto({
@JsonKey(name: "response_time_utc") required DateTime responseTime,
@JsonKey(name: 'verified') required List<_SwapAssetDto> verifiedAssets,
@JsonKey(name: 'unverified') required List<_SwapAssetDto> unverifiedAssets,
}) = __SwapAssetsDto;
factory _SwapAssetsDto.fromJson(Map<String, dynamic> json) => _$SwapAssetsDtoFromJson(json);
}
@Freezed(fromJson: false, toJson: false)
class SwapAssets with _$SwapAssets {
const SwapAssets._();
const factory SwapAssets({
required DateTime responseTime,
required List<SwapAsset> assets,
}) = _SwapAssets;
factory SwapAssets.fromJson(Map<String, dynamic> json) {
return SwapAssets._fromDto(_SwapAssetsDto.fromJson(json));
}
factory SwapAssets._fromDto(_SwapAssetsDto dto) {
return SwapAssets(responseTime: DateTime.now(), assets: []);
}
}
@freezed
class SwapAsset with _$SwapAsset {
const SwapAsset._();
const factory SwapAsset({
required String dotSeparatedUnit,
required String unit,
required bool verified,
required bool isAda,
required int? decimals,
required String policy,
required String hexAssetName,
required String displayTitle,
required String? displaySubtitle,
required String imageModel,
}) = _SwapAsset;
factory SwapAsset.fromJson(Map<String, dynamic> json) => SwapAsset._fromDto(
_$SwapAssetDtoFromJson(json),
false,
);
factory SwapAsset._fromDto(_SwapAssetDto dto, bool verified) => SwapAsset._fromData(
unit: dto.unit,
name: dto.name,
ticker: dto.ticker,
decimals: dto.decimals,
verified: verified,
);
}
P.s. this is a slightly shorter/simplified version of my actual code.