0

I've created an array of list of objects along with strings. and in my API i tried to use post method. the post method writes on the server but when i try to read the value with my model it throws an error of Unhandled Exception: NoSuchMethodError: Class 'bool' has no instance method 'forEach'.. I'm using a complex JSON structure.

Model class

class Client {
  List<Clients>? clients;

  Client({required this.clients});

  Client.fromJson(Map<String, dynamic> json) {
    if (json['clients'] != null) {
      clients = <Clients>[];
      json['clients'].forEach((v) {
        clients?.add(new Clients.fromJson(v));
      });
    }
  }

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

class Clients {
  int? id;
  String? name;
  String? address;
  String? tinNo;
  String? status;
  String? createdAt;
  String? updatedAt;
  List<Phones>? phones;

  Clients(
      {this.id,
      this.name,
      this.address,
      this.tinNo,
      this.status,
      this.createdAt,
      this.updatedAt,
      this.phones});

  Clients.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    name = json['name'];
    address = json['address'];
    tinNo = json['tin_no'];
    status = json['status'];
    createdAt = json['created_at'];
    updatedAt = json['updated_at'];
    if (json['phones'] != null) {
      phones = <Phones>[];
      json['phones'].forEach((v) {
        phones!.add(new Phones.fromJson(v));
      });
    }
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['name'] = this.name;
    data['address'] = this.address;
    data['tin_no'] = this.tinNo;
    data['status'] = this.status;
    data['created_at'] = this.createdAt;
    data['updated_at'] = this.updatedAt;
    if (this.phones != null) {
      data['phones'] = this.phones!.map((v) => v.toJson()).toList();
    }
    return data;
  }
}

class Phones {
  int? id;
  String? clientId;
  Null? supplierId;
  Null? companyId;
  String? phoneNumber;
  String? email;
  String? model;
  String? category;
  String? createdAt;
  String? updatedAt;

  Phones(
      {this.id,
      this.clientId,
      this.supplierId,
      this.companyId,
      this.phoneNumber,
      this.email,
      this.model,
      this.category,
      this.createdAt,
      this.updatedAt});

  Phones.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    clientId = json['client_id'];
    supplierId = json['supplier_id'];
    companyId = json['company_id'];
    phoneNumber = json['phone_number'];
    email = json['email'];
    model = json['model'];
    category = json['category'];
    createdAt = json['created_at'];
    updatedAt = json['updated_at'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['client_id'] = this.clientId;
    data['supplier_id'] = this.supplierId;
    data['company_id'] = this.companyId;
    data['phone_number'] = this.phoneNumber;
    data['email'] = this.email;
    data['model'] = this.model;
    data['category'] = this.category;
    data['created_at'] = this.createdAt;
    data['updated_at'] = this.updatedAt;
    return data;
  }
}

and my API call

Future createClient(String name, String address, String tin_no, String status,
    List<Phones> phones) async {
  Uri url = Uri.parse("${BASE_URL}client");
  ClientController clientController = Get.put(ClientController());

  final response = await http
      .post(url,
          headers: <String, String>{
            'Content-Type': 'application/json; charset=UTF-8',
            'Authorization': 'Bearer $TOKEN',
          },
          body: jsonEncode(<String, dynamic>{
            'name': name,
            'address': address,
            'tin_no': tin_no,
            'status': status,
            'phones': phones.toList()
          }))
      .then((value) {
    if (value.statusCode == 200) {
      clientController.setclientPress(true);
      print(Clients.fromJson(json.decode(value.body)));
      clientController.createClient(Clients.fromJson(json.decode(value.body)));
      Get.back();
      clientController.setclientPress(false);
      print("success");
    } else if (value.statusCode == 500) {
      Get.snackbar("Data Exists", "The data you provided already exists.");
    } else {
      print("failed ${value.body}");
      print("...............$phones");
    }
    print(value.body);
  });
}

button for calling

onPressed: () async {

   List<Phones> phones = [];
   for (int i = 0; i < _groupControllers.length; i++) {
     String _phone = _groupControllers[i].phone.text;
     String _email = _groupControllers[i].email.text;
     String _category = "Reception";
     //_groupControllers[i].selectedCatagory.toString();

     setState(() {
      phones.add(Phones(
      phoneNumber: _phone,
      email: _email,
      category: _category));
     });
   }
  if (_formKey.currentState!.validate()) {
                      
     await createClient(
       nameController.text,
       addressController.text,
       tinController.text,
       idx.toString(),
       phones);
                      
     }
}

response is

    {
    "client": {
        "name": "trerw",
        "address": "trwrte",
        "tin_no": "45363524",
        "status": "1",
        "updated_at": "2022-10-01T12:44:19.000000Z",
        "created_at": "2022-10-01T12:44:19.000000Z",
        "id": 34
    },
    "phones": true,
    "message": "Client created successfully"
}

the phones should not return bool. it should be as follows

{
    "client": {
        "id": 1,
        "name": "Muhammed",
        "address": "Mexico",
        "tin_no": "123456789",
        "status": "1",
        "created_at": "2022-09-09T08:44:18.000000Z",
        "updated_at": "2022-09-09T08:44:18.000000Z",
        "phones": [
            {
                "id": 2,
                "client_id": "1",
                "supplier_id": null,
                "company_id": null,
                "phone_number": "0911112222",
                "email": "[email protected]",
                "model": "Client",
                "category": null,
                "created_at": "2022-09-09T08:44:18.000000Z",
                "updated_at": "2022-09-09T08:44:18.000000Z"
            }
        ]
    }
}

what am i doing wrong?

3
  • could you please add your api response? Commented Oct 1, 2022 at 12:06
  • @eamirho3ein the response is {"client":{"name":"trerw","address":"trwrte","tin_no":"45363524","status":"1","updated_at":"2022-10-01T12:44:19.000000Z","created_at":"2022-10-01T12:44:19.000000Z","id":34},"phones":true,"message":"Client created successfully"} Commented Oct 1, 2022 at 12:44
  • @eamirho3ein the phones should be a list of objects not a bool Commented Oct 1, 2022 at 12:46

1 Answer 1

3

As you can see in your api response:

{"client":{"name":"trerw","address":"trwrte","tin_no":"45363524","status":"1","updated_at":"2022-10-01T12:44:19.000000Z","created_at":"2022-10-01T12:44:19.000000Z","id":34},"phones":true,"message":"Client created successfully"}

phones is a bool variable, but in Clients.fromJson you consider it as list.So i think you send phones wrong in your api response.

Also you are passing phones in a wrong way to api body, try this:

body: jsonEncode(<String, dynamic>{
            'name': name,
            'address': address,
            'tin_no': tin_no,
            'status': status,
            'phones': phones.map((e) => e.toJson()).toList();
          }))
Sign up to request clarification or add additional context in comments.

8 Comments

i send the phone data in List<Phones>. not bool
but after it sends it to the server it is in the correct order and phones is an array
as you can see you receive a bool instead of list. you should looking for issue in backend logic, this is not flutter issue,
so am i passing the list correctly to the body of request?
I don't think it relate to the request body. as I said before its relate to your backend logic. Although you are passing phones wrong in your api body, can you provide approve sample of your api body?
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.