Home > Software engineering >  Error saving data in Rest API - Flutter/Dart
Error saving data in Rest API - Flutter/Dart

Time:12-20

I have the following return from the API (which I can return normally on my main screen!):

    [
    {
        "id": 1,
        "obs": "",
        "dataVcto": "2022-11-02T00:00:00.000Z",
        "valor": 200,
        "idTPRD": 1,
        "dataLcto": "2022-11-02T00:00:00.000Z",
        "status": "A",
        "idUSRS": 1,
        "dC": "C",
        "idCTCT": 1,
        "tPRD_Nome": "FRETE",
        "uSRS_Nome": "Heugenio",
        "cTCT_Nome": "Frete p\/Terceiros"
    },
    {
        "id": 2,
        "obs": "Maquina Vilmar",
        "dataVcto": "2022-12-01T00:00:00.000Z",
        "valor": 300,
        "idTPRD": 1,
        "dataLcto": "2022-11-05T00:00:00.000Z",
        "status": "A",
        "idUSRS": 1,
        "dC": "C",
        "idCTCT": 1,
        "tPRD_Nome": "FRETE",
        "uSRS_Nome": "Heugenio",
        "cTCT_Nome": "Frete p\/Terceiros"
    },
    {
        "id": 5,
        "obs": "",
        "dataVcto": "2022-12-01T00:00:00.000Z",
        "valor": 200,
        "idTPRD": 2,
        "dataLcto": "2022-11-05T00:00:00.000Z",
        "status": "A",
        "idUSRS": 1,
        "dC": "D",
        "idCTCT": 1,
        "tPRD_Nome": "OLEO",
        "uSRS_Nome": "Heugenio",
        "cTCT_Nome": "Frete p\/Terceiros"
    }
]

My model class (Automatically generated on a website that creates these classes):

    class LancamentosModel {
  

    int? id;
      String? obs;
      String? dataVcto;
      int? valor;
      int? idTPRD;
      String? dataLcto;
      String? status;
      int? idUSRS;
      String? dC;
      int? idCTCT;
      String? tPRDNome;
      String? uSRSNome;
      String? cTCTNome;
    
      LancamentosModel(
          {this.id,
            this.obs,
            this.dataVcto,
            this.valor,
            this.idTPRD,
            this.dataLcto,
            this.status,
            this.idUSRS,
            this.dC,
            this.idCTCT,
            this.tPRDNome,
            this.uSRSNome,
            this.cTCTNome});
    
      LancamentosModel.fromJson(Map<String, dynamic> json) {
        id = json['id'];
        obs = json['obs'];
        dataVcto = json['dataVcto'];
        valor = json['valor'];
        idTPRD = json['idTPRD'];
        dataLcto = json['dataLcto'];
        status = json['status'];
        idUSRS = json['idUSRS'];
        dC = json['dC'];
        idCTCT = json['idCTCT'];
        tPRDNome = json['tPRD_Nome'];
        uSRSNome = json['uSRS_Nome'];
        cTCTNome = json['cTCT_Nome'];
      }
    
      Map<String, dynamic> toJson() {
        final Map<String, dynamic> data = new Map<String, dynamic>();
        data['id'] = this.id;
        data['obs'] = this.obs;
        data['dataVcto'] = this.dataVcto;
        data['valor'] = this.valor;
        data['idTPRD'] = this.idTPRD;
        data['dataLcto'] = this.dataLcto;
        data['status'] = this.status;
        data['idUSRS'] = this.idUSRS;
        data['dC'] = this.dC;
        data['idCTCT'] = this.idCTCT;
        data['tPRD_Nome'] = this.tPRDNome;
        data['uSRS_Nome'] = this.uSRSNome;
        data['cTCT_Nome'] = this.cTCTNome;
        return data;
      }
    }

When I try to save the data in this listing by the application, I get this exception:

Error: Expected a value of type 'String', but got one of type 'int'

action on the button to save the data:


   ElevatedButton(
                       onPressed: () {
   
                         var dados = LancamentosModel(
                           dataLcto: _dataController.text.toString(),
                           dataVcto: _vencimentocontroller.text.toString(),
                           obs: _obsController.text.toString(),
                           valor: int.tryParse(_valorController.text.toString()),
                           status: _controller.toString(),
                           dC: _type.toString(),
                           uSRSNome: "Felippe",
                           cTCTNome: "Frete",
                           tPRDNome: "Óleo",
                           id: 2,
                           idCTCT: 1,
                           idTPRD: 1,
                           idUSRS: 1
                         );
   
                         salvarDados(dados);
   
                       },

salvarDados() method:



    Future salvarDados(LancamentosModel dados) async {
   
       var resp = await http.post(
           Uri.parse("http://hjsystems.dynns.com:8085/GravaLancamentos"),
           headers: {
             "Authorization": "Basic *******************"
           },
           body: dados.toJson());
   
       if (resp.statusCode != 200) Exception("Erro ao salvar");
     }

CodePudding user response:

You toJson() method does not actually produce JSON, it just produces a data structure that can be serialized as JSON. That is correct. This is the pattern we use.

But you need to actually serialize it as JSON text:

body: jsonEncode(dados.toJson()),
  • Related