flutterjsonserializerfreezed

missing methods to override in Freezed Flutter


I am trying to use freeze with json_serializer . Response from Api is

{
    "CustomData": {
        "message": "OTP sent successfully",
        "msisdn": "1273"
    }
}

Now as this response comes from sendOTP api . so I do it like

import 'package:freezed_annotation/freezed_annotation.dart';

part 'send_otp_response.freezed.dart';
part 'send_otp_response.g.dart';

@freezed
class SendOtpResponse with _$SendOtpResponse {
  const factory SendOtpResponse({
    @JsonKey(name: 'CustomData') required CustomData customData,
  }) = _SendOtpResponse;

  factory SendOtpResponse.fromJson(Map<String, dynamic> json) =>
      _$SendOtpResponseFromJson(json);
}

@freezed
class CustomData with _$CustomData {
  const factory CustomData({
    @Default('') String message,
    @Default('') String msisdn,
  }) = _CustomData;

  factory CustomData.fromJson(Map<String, dynamic> json) =>
      _$CustomDataFromJson(json);
}

and I also run the command

flutter packages pub run build_runner watch --delete-conflicting-outputs

But when I hover over SendOtpResponse . It shows an error

Missing concrete implementations of '_$SendOtpResponse. toJson' and 'getter _$SendOtpResponse. customData'. (Documentation) Try implementing the missing methods, or make the class abstract.

and when I hover customData it says

Missing concrete implementations of '_$CustomData. toJson', 'getter _$CustomData. message', and 'getter _$CustomData. msisdn'. (Documentation) Try implementing the missing methods, or make the class abstract.

for customData . It says two missing methods and for sendOTPResponse it says 3 missing methods.

I also make sure the name of the class is send_otp_response.dart


Solution

  • if you using freezed 3.0.0 or newer make you class to abstract class can fix this issue

    you can read in this patch note in other change section
    ref. https://pub.dev/packages/freezed/changelog#300---2025-02-25

    import 'package:freezed_annotation/freezed_annotation.dart';
    
    part 'send_otp_response.freezed.dart';
    part 'send_otp_response.g.dart';
    
    @freezed
    abstract class SendOtpResponse with _$SendOtpResponse {
      const factory SendOtpResponse({
        @JsonKey(name: 'CustomData') required CustomData customData,
      }) = _SendOtpResponse;
    
      factory SendOtpResponse.fromJson(Map<String, dynamic> json) => _$SendOtpResponseFromJson(json);
    }
    
    @freezed
    abstract class CustomData with _$CustomData {
      const factory CustomData({
        @Default('') String message,
        @Default('') String msisdn,
      }) = _CustomData;
    
      factory CustomData.fromJson(Map<String, dynamic> json) => _$CustomDataFromJson(json);
    }