skip to Main Content
[{
    "getOtherCategoryCompaniesList_resp": [{
            "messageInfo": {
                "message_text": "Other Services List.",
                "alert_title": "Services List",
                "product_category_id": "56",
                "Success": "1"
            }
        },
        {
            "companies": [{
                    "product_type_id": 1946,
                    "company_id": 609,
                    "product_name": "_All Puja",
                    "product_company_logo": "https://bhagva.in/imgs/icons/prod_category/logo.png"
                },
                {
                    "product_type_id": 1753,
                    "company_id": 535,
                    "product_name": "Aasa Maai Vrat Katha",
                    "product_company_logo": "https://bhagva.in/imgs/icons/prod_category/logo.png"
                }
            ]
        }
    ]
}]

This is my model class which i have generate online from json to pojo in dart

class Poojlistmodel {
  int? producttypeid;
  int? companyid;
  String? productname;
  String? productcompanylogo;

  Poojlistmodel(
      {this.producttypeid,
      this.companyid,
      this.productname,
      this.productcompanylogo});

  Poojlistmodel.fromJson(Map<String, dynamic> json) {
    producttypeid = json['product_type_id'];
    companyid = json['company_id'];
    productname = json['product_name'];
    productcompanylogo = json['product_company_logo'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = Map<String, dynamic>();
    data['product_type_id'] = producttypeid;
    data['company_id'] = companyid;
    data['product_name'] = productname;
    data['product_company_logo'] = productcompanylogo;
    return data;
  }
}

class GetOtherCategoryCompaniesListResp {
  MessageInfo? messageInfo;
  List<Poojlistmodel?>? companies;

  GetOtherCategoryCompaniesListResp({this.messageInfo, this.companies});

  GetOtherCategoryCompaniesListResp.fromJson(Map<String, dynamic> json) {
    messageInfo = json['messageInfo'] != null
        ? MessageInfo?.fromJson(json['messageInfo'])
        : null;
    if (json['companies'] != null) {
      companies = <Poojlistmodel>[];
      json['companies'].forEach((v) {
        companies!.add(Poojlistmodel.fromJson(v));
      });
    }
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = Map<String, dynamic>();
    data['messageInfo'] = messageInfo!.toJson();
    data['companies'] =
        companies != null ? companies!.map((v) => v?.toJson()).toList() : null;
    return data;
  }
}

class MessageInfo {
  String? messagetext;
  String? alerttitle;
  String? productcategoryid;
  String? success;

  MessageInfo(
      {this.messagetext,
      this.alerttitle,
      this.productcategoryid,
      this.success});

  MessageInfo.fromJson(Map<String, dynamic> json) {
    messagetext = json['message_text'];
    alerttitle = json['alert_title'];
    productcategoryid = json['product_category_id'];
    success = json['Success'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = Map<String, dynamic>();
    data['message_text'] = messagetext;
    data['alert_title'] = alerttitle;
    data['product_category_id'] = productcategoryid;
    data['Success'] = success;
    return data;
  }
}

class Root {
  List<GetOtherCategoryCompaniesListResp?>? getOtherCategoryCompaniesListresp;

  Root({this.getOtherCategoryCompaniesListresp});

  Root.fromJson(Map<String, dynamic> json) {
    if (json['getOtherCategoryCompaniesList_resp'] != null) {
      getOtherCategoryCompaniesListresp = <GetOtherCategoryCompaniesListResp>[];
      json['getOtherCategoryCompaniesList_resp'].forEach((v) {
        getOtherCategoryCompaniesListresp!
            .add(GetOtherCategoryCompaniesListResp.fromJson(v));
      });
    }
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = Map<String, dynamic>();
    data['getOtherCategoryCompaniesList_resp'] =
        getOtherCategoryCompaniesListresp != null
            ? getOtherCategoryCompaniesListresp!
                .map((v) => v?.toJson())
                .toList()
            : null;
    return data;
  }
}

This code i am using to network hit

class MySpecilistRepo {
  final BaseApiServices _apiServices = NetworkApiService();

  Future<dynamic> dashboardApi(String userId,String productCategoryId) async {
    try {
      var params = {
        "getOtherCategoryCompaniesList_req": [
          {
            "requestInfo": {
              "user_id": userId,
              "product_category_id": productCategoryId
            }
          }
        ]
      };

      dynamic response =
          await _apiServices.authApiResponse(AppUrl.GET_LIST_OF_POOJA, params);
      print("dashboard $response");
      return Poojlistmodel.fromJson(response);
    } catch (e) {
      rethrow;
    }
  }
}

When i run code i am getting exception that type ‘_Map<String, List<Map<String, Map<String, String>>>>’ is not a subtype of type ‘String’ i don’t know where i am doing mistake while i have generate model class using online please help me where i am doing wrong i trying first time network hit in flutter .

Thanks

2

Answers


  1. change

    return Poojlistmodel.fromJson(response);
    

    to

      final root = Root.fromJson(jsonDecode(response)[0]);
      final List<Poojlistmodel?> models = root.getOtherCategoryCompaniesListresp![1]!.companies!;
      print(models.map((e) => e?.productname)); // this will print _All Puja, Aasa Maai Vrat Katha
      return models; 
    
    Login or Signup to reply.
  2. This is little nested data, but it will easy to understand the process. First get the data, then get the companies. You can play with simple json .

      Future<dynamic> dashboardApi(String userId, String productCategoryId) async {
        try {
          dynamic response =
              await _apiServices.authApiResponse(AppUrl.GET_LIST_OF_POOJA, params);
          // dynamic response = fixture('data.json');
          
          final data = jsonDecode(response) as List? ?? [];
          final companies = data.map((e) => Poojlistmodel.fromJson(e)).toList();//it would be nice to call it fromMap instead
          print(companies.length); 
          // this returns list of model. 
          // you can get the first like companies.first, 
          // also the check list is empty or not.
          
        } catch (e) {
          rethrow;
        }
      }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search