0

Kindly assist. I am attempting to create a model using the JSON to Dart tool https://app.quicktype.io/

The aim is to retrieve a list of all companies (with their properties) from a JSON API.

I am getting the below errors:

  • the argument type 'dynamic' can't be assigned to the parameter type Map<String, dynamic>
  • the argument type 'dynamic' can't be assigned to the parameter type Iterable
  • the argument type 'dynamic' can't be assigned to the parameter type Int

Please refer to the image below. the argument type 'dynamic' can't be assigned to the parameter type...

Thank you.

2
  • can you post the json response ?
    – Sal Man
    Apr 9, 2021 at 11:31
  • { "companies": [ { "id": 19, "name": "Equity" }, { "id": 1, "name": "King Pastry" }, { "id": 5, "name": "Sonas" } ] }
    – elsurha
    Apr 9, 2021 at 11:47

1 Answer 1

0

I have tested this class and it works without errors

import 'dart:convert';

CompanyModel companyModelFromJson(String str) => CompanyModel.fromJson(json.decode(str));

String companyModelToJson(CompanyModel data) => json.encode(data.toJson());

class CompanyModel {
    CompanyModel({
        this.companies,
    });

    List<Company> companies;

    factory CompanyModel.fromJson(Map<String, dynamic> json) => CompanyModel(
        companies: List<Company>.from(json["companies"].map((x) => Company.fromJson(x))),
    );

    Map<String, dynamic> toJson() => {
        "companies": List<dynamic>.from(companies.map((x) => x.toJson())),
    };
}

class Company {
    Company({
        this.id,
        this.name,
    });

    int id;
    String name;

    factory Company.fromJson(Map<String, dynamic> json) => Company(
        id: json["id"],
        name: json["name"],
    );

    Map<String, dynamic> toJson() => {
        "id": id,
        "name": name,
    };
}

As alternative you can use this code from JsonToDart

class CompanyModel {
  List<Companies> companies;

  CompanyModel({this.companies});

  CompanyModel.fromJson(Map<String, dynamic> json) {
    if (json['companies'] != null) {
      companies = new List<Companies>();
      json['companies'].forEach((v) {
        companies.add(new Companies.fromJson(v));
      });
    }
  }

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

class Companies {
  int id;
  String name;

  Companies({this.id, this.name});

  Companies.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    name = json['name'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['name'] = this.name;
    return data;
  }
}
1
  • Thank you. Should have mentioned though, I tested JsonToDart as well. Which flutter version are you running? I am wondering if it has to do with my version. I am using: Flutter 2.0.3 Dart 2.12.2
    – elsurha
    Apr 9, 2021 at 13:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.