vendredi 1 juillet 2022

Flutter : checkbox add array data to hit POST API, and error checkbox

I have problem with checkbox to add parameter hit to POST data for API.

This is POST data sent when I clicked Save Button :

Map<String, String> params = {
        'pet_name': petNameCtrl.text,
        'pet_healths[0]': '3', //manual
        'pet_healths[1]': '4', //manual
    };
    
    final response = await http.post(
      Uri.parse(
        "http://localhost:8000/api/v1/pet",
      ),
      headers: {'Authorization': 'Bearer $token'},
      body: params,
    );
}

This is how my checkbox data called from API and the widget :

List<PetHealthModel> petHealths = [];
bool selectedPetHealth = false;


  void fetchPetData() {
    getDataHealth();
  }

  void getDataHealth() async {
    final response = await http.get(
      Uri.parse("http://localhost:8000/api/v1/pet_health"),
    );
    final metadata = PetHealthsMetadata.fromJson(jsonDecode(response.body));
    setState(() => petHealths = metadata.data);
  }
  
  
petHealths.isNotEmpty
    ? Padding(
    padding: const EdgeInsets.all(8.0),
    child: Column(
        children: petHealths.map((e) {
            return CheckboxListTile(
                title: Text(e.healthStatus),
                value: selectedPetHealth,
                onChanged: (bool? value) {
                    setState(() {
                        selectedPetHealth = value!;
                    });
                }
            );
        }).toList(),
    ),
)

*btw I have secondary problem that if I select one of the checkbox, all checkbox will selected too. here's the picture

enter image description here

Please help, get stuck here for many days.

here's the sample for API :

enter image description here

PetHealthModel :

class PetHealthModel {
  PetHealthModel({
    required this.id,
    required this.healthStatus,
  });

  final int id;
  final String healthStatus;

  factory PetHealthModel.fromJson(Map<String, dynamic> json) => PetHealthModel(
        id: json["id"],
        healthStatus: json["health_status"],
      );

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


class PetHealthsMetadata {
  PetHealthsMetadata({
    required this.data,
    required this.code,
    required this.msg,
  });

  final List<PetHealthModel> data;
  final int code;
  final String msg;

  factory PetHealthsMetadata.fromJson(Map<String, dynamic> json) =>
      PetHealthsMetadata(
        data: List<PetHealthModel>.from(
            json["data"].map((x) => PetHealthModel.fromJson(x))),
        code: json["code"],
        msg: json["msg"],
      );

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

*updated

Here is what it printed everytime the checkbox changed :

enter image description here

For final problem is how to made it auto add to Map<String, String> params ? see params code above




Aucun commentaire:

Enregistrer un commentaire