freezed package class return - "type 'Null' is not a subtype of type 'String' in type cast"

48 views Asked by At

I am new to Dart and OOP. I am using freezed package to create my Model classes. I cant get the response to return in my function, always goes to catch(error)

here is my code

static Future<SearchResponse?> searchImageBasedCatagory(
  File file,
  {requiresToken = true}) async {
try {
  String fileExtension = p.extension(file.path);
  List<String> finalExtension = fileExtension.split('.');
  var formData = FormData.fromMap({
    'searchImage': await MultipartFile.fromFile(
      file.path,
      contentType: MediaType("image", finalExtension[1]),
    ),
  });
  var res = await NetworkCommon.dio.post(
    EndPoints.imageBasedSearch,

    options: Options(
      headers: {'requires_token': true},
    ),
    data: formData,
  );
  var response = SearchResponse.fromJson(res.data);
  return response;
} catch (error) {
  return null;
 }
}

My models

SearchResponse

@freezed
 class SearchResponse with _$SearchResponse {
   factory SearchResponse({
     required String message,
     required int length,
     required List<CameraImageCatagoryItem> data,
}) = _SearchResponse;

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

CameraImageCatagoryItem

@freezed
class CameraImageCatagoryItem with _$CameraImageCatagoryItem {
  factory CameraImageCatagoryItem(
  {
    int? width,
    int? height,
    required List<BoundBoxItem> boundBox}) = _CameraImageCatagoryItem;

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

BoundBoxItem

@freezed
class BoundBoxItem with _$BoundBoxItem {
   factory BoundBoxItem({
     required String catagory,
     int? prob,
     int? area,
     required List<int> bound_box,
     List<int>? expand_by_bound_box,
     required String gender
}) = _BoundBoxItem;

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

Data Sample

 {
"message": "success",
"length": 1,
"data": [
    {
        "width": 299,
        "height": 514,
        "boundBox": [
            {
                "category": "t-shirts",
                "prob": 0.9578810930252075,
                "area": 0.5919081764116445,
                "bound_box": [
                    25,
                    139,
                    299,
                    471
                ],
                "expand_by_bound_box": [
                    0,
                    106,
                    298,
                    504
                ],
                "gender": "men"
            }
        ]
    }
]
 }

The code always end in error block and message is always type 'Null' is not a subtype of type 'String' in type cast

enter image description here

1

There are 1 answers

2
Dhafin Rayhan On

There are some problems in this code:

factory BoundBoxItem({
  required String catagory,
  int? prob,
  int? area,
  // ...
}) = _BoundBoxItem;
  1. You named the parameter catagory (4th letter is 'a'), but in the data JSON it's category (4th letter is 'e').
  2. You give prob and area the type of int, but in the JSON it's a double. Consider using num to handle both int and double.