Search code examples
flutterdartretrofitpojo

the method 'fromjson' isn't defined for the type 'type'


The method 'fromJson' isn't defined for the type 'Type'.
Try correcting the name to the name of an existing method, or defining a method named 'fromJson'

this below code is in Retrofit.g.dart file

@override
  Future<Map<String, dynamic>> signupCustomerRegistration(customerReg) async {
    ArgumentError.checkNotNull(customerReg, 'customerReg');
    const _extra = <String, dynamic>{};
    final queryParameters = <String, dynamic>{};
    final _data = <String, dynamic>{};
    _data.addAll(customerReg?.toJson() ?? <String, dynamic>{});
    final _result = await _dio.request<Map<String, dynamic>>(
        '/api/API/CustomerSignUp',
        queryParameters: queryParameters,
        options: RequestOptions(
            method: 'POST',
            headers: <String, dynamic>{},
            extra: _extra,
            baseUrl: baseUrl),
        data: _data);
    var value = _result.data.map((k, dynamic v) =>
        MapEntry(k, dynamic.fromJson(v as Map<String, dynamic>)));

    return value;
  }

My Model File code is below:

// To parse this JSON data, do
//
//     final customerReg = customerRegFromJson(jsonString);

import 'dart:convert';

import 'package:json_annotation/json_annotation.dart';

part 'CustomerRegModel.g.dart';


@JsonSerializable()
class CustomerRegModel {
  CustomerRegModel({
    this.custUid,
    this.appname,
    this.blacklist,
    this.custEmail,
    this.custName,
    this.custPhone,
    this.fcmToken,
    this.password,
    this.agentnin,
    this.source,
    this.signupDate,
    this.commStartTime,
    this.commEndTime,
    this.commMaxValue,
    this.commMinValue,
    this.commDownValue,
    this.walletamount,
  });

  String custUid;
  String appname;
  bool blacklist;
  String custEmail;
  String custName;
  String custPhone;
  String fcmToken;
  String password;
  String agentnin;
  String source;
  int signupDate;
  int commStartTime;
  int commEndTime;
  int commMaxValue;
  int commMinValue;
  int commDownValue;
  int walletamount;

  factory CustomerRegModel.fromJson(Map<String, dynamic> json) => _$CustomerRegModelFromJson(json);
  Map<String, dynamic> toJson() => _$CustomerRegModelToJson(this);
  
  CustomerRegModel customerRegModelFromJson(String str) => CustomerRegModel.fromJson(json.decode(str));
  String customerRegModelToJson(CustomerRegModel data) => json.encode(data.toJson());
}

Tried:

  • Invalidate Caches and Restart.
  • Deleting .g.dart files and recreating it.
  • Have implemented same Code for other models with retrofit and working fine just not working on this.

Solution

  • Ok so after doing research and creating an issue in Github Retrofit (https://github.com/trevorwang/retrofit.dart/issues/327) which they didn't respond, i went for searching parsing Json to Map and i found solution to my problem.

    i.e:

    For Retrofit Configuration:-

      @GET("/api/API/CustomerLogin")
      Future<String> loginCustomer(@Query('id') String email_or_number,
      @Query('pass') String pass, @Query('check') String check);
    

    Notice above that i used String as response. Now i will convert String into Map using json.decode method.

    final response = await client.loginCustomer(email_or_number,pass,check);
    final Map<String,dynamic> map = json.decode(response);