Error when converting json with object lists to a Dart object

Asked

Viewed 409 times

0

Good morning to you all! I am getting the following error while trying to convert a json to Dart. [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: type '_Internallinkedhashmap' is not a subtype of type 'String'

My code

import 'horarios_modelo.dart';
import 'pontos_modelo.dart';

class UsuarioModelo {
  final String nome;
  final String pis;
  final String cargo;
  final bool admin;
  final String cnpj;
  final String centroCusto;
  final List<PontoModelo> pontos;
  final List<HorarioModelo> horarios;

  UsuarioModelo({
    this.nome,
    this.pis,
    this.cargo,
    this.admin,
    this.cnpj,
    this.centroCusto,
    this.pontos,
    this.horarios,
  });

  factory UsuarioModelo.fromJson(Map<String, dynamic> parsedJson){
    var listP = parsedJson['pontos'] as List;
    print(listP.runtimeType);
    List<PontoModelo> pontosList = listP.map((i) => PontoModelo.fromJson(i)).toList();

    var listH = parsedJson['horarios'] as List;
    print(listH.runtimeType);
    List<HorarioModelo> horariossList = listH.map((j) => HorarioModelo.fromJson(j)).toList();

    return UsuarioModelo(
        nome: parsedJson['name'],
        pis: parsedJson['pis'],
        cargo: parsedJson['cargo'],
        admin: parsedJson['admin'],
        cnpj: parsedJson['cnpj'],
        centroCusto: parsedJson['centroCusto'],
        pontos: pontosList,
        horarios: horariossList
    );
  }

  Map<String, dynamic> toJson() => {
    'nome': nome,
    'pis': pis,
    'cargo': cargo,
    'admin': admin,
    'cnpj': cnpj,
    'centroCusto': centroCusto,
    'pontos': new List<dynamic>.from(pontos.map((x) => x.runtimeType)),
    'horarios': new List<dynamic>.from(horarios.map((x) => x.runtimeType)),
  };
}


class HorarioModelo {
  final String diaDaSemana;
  final String entrada1;
  final String saida1;
  final String entrada2;
  final String saida2;
  final String entrada3;
  final String saida3;
  final bool flexivel;
  final bool ehFolga;

  HorarioModelo({
    this.diaDaSemana,
    this.entrada1,
    this.saida1,
    this.entrada2,
    this.saida2,
    this.entrada3,
    this.saida3,
    this.flexivel,
    this.ehFolga,
  });

  factory HorarioModelo.fromJson(Map<String, dynamic> parsedJson){
    return HorarioModelo(
        diaDaSemana: parsedJson['diaDaSemana'],
        entrada1: parsedJson['entrada1'],
        saida1: parsedJson['saida1'],
        entrada2: parsedJson['entrada2'],
        saida2: parsedJson['saida2'],
        entrada3: parsedJson['entrada3'],
        saida3: parsedJson['saida3'],
        flexivel: parsedJson['flexivel'],
        ehFolga: parsedJson['ehFolga']
    );
  }
}

class PontoModelo {
  final String dia;
  final String entrada1;
  final String saida1;
  final String entrada2;
  final String saida2;
  final String entrada3;
  final String saida3;

  PontoModelo({
    this.dia,
    this.entrada1,
    this.saida1,
    this.entrada2,
    this.saida2,
    this.entrada3,
    this.saida3,
  });

  factory PontoModelo.fromJson(Map<String, dynamic> parsedJson){
    return PontoModelo(
        dia: parsedJson['dia'],
        entrada1: parsedJson['entrada1'],
        saida1: parsedJson['saida1'],
        entrada2: parsedJson["entrada2"],
        saida2: parsedJson['saida2'],
        entrada3: parsedJson['entrada3'],
        saida3: parsedJson['saida3']
    );
  }
}

Método para pegar o dados
Future<dynamic> getDado(JsonUser user) async {
    var url = "http://200.201.144.179:8080/ikponto";
    var uri = '/servico/sessao/funcionario/obter-dados-gerais';
    var token = user.token;
    //envia enviado no Header com a chave
    //Authorization e valor Bearer {token} .
    Dio dio = new Dio();
    dio.options.headers = {
      'Authorization': 'Bearer ' + token,
      'idFuncionario': user.id
    };
    dio.options.contentType = ContentType.parse("application/x-www-form-urlencoded");
    Response  response =  await dio. post (url+uri, data : {
      "idFuncionario":user.id
    });
    var body = response;
    print(body);
    var responseJson = json.decode(response.data);
    return responseJson;
  }
  
  Meu login, ele passa perfeitamene mas na hora de converter para o usuario ele me retorna esse erro.
  child: RaisedButton(
                              child: Text("Login"),
                              color: Colors.red,
                              onPressed:
                              () async {
                                var respLogin = await _loginServico.login(_emailController.text, _senhaController.text);
                                JsonUser user = JsonUser.fromJson(respLogin);
                                var respDados =  await _loginServico.getDado(user);
                                usuario = UsuarioModelo.fromJson(respDados);
                                print(usuario.nome);
                                if (usuario != null) {
                                 Navigator.of(context).push(MaterialPageRoute<Null>(
                                      builder: (BuildContext context) {
                                        return new HomePage(
                                          usuario: usuario,
                                        );
                                      }));
                                } else {
                                  Scaffold.of(context).showSnackBar(
                                      SnackBar(content: Text("E-mail ou Senha não confere")));
                                }
                              },
                            ),

  • It would be easier if you added the json which is being returned, also highlight the exact line/command where the error occurs.

  • Okay. var respDados = await _loginServico.getDado(user);&#xA; usuario = UsuarioModelo.fromJson(respDados);

1 answer

-1

Resposta JSON :
{
"nome": "Adilson Junior",
"pis":"1234567890",
"cargo": "PROGRAMADOR JR III",
"admin": false,
"cnpj": "12004232000105",
"centroCusto": "DESENVOLVIMENTO",
"pontos": [
{
"dia": "01/01/2019",
"entrada1": null,
"saida1": null,
"entrada2": null,
"saida2": null,
"entrada3": null,
"saida3": null
},
{
"dia": "02/01/2019",
"entrada1": "02/01/2019 07:58",
"saida1": "02/01/2019 12:07",
"entrada2": "02/01/2019 13:07",
"saida2": "02/01/2019 18:03",
"entrada3": null,
"saida3": null
},
{
"dia": "03/01/2019",
"entrada1": "03/01/2019 07:57",
"saida1": "03/01/2019 12:21",
"entrada2": "03/01/2019 13:23",
"saida2": "03/01/2019 18:00",
"entrada3": null,
"saida3": null
}
],
"horarios": [
{
"diaDaSemana": "Seg",
"entrada1": "08:00",
"saida1": "12:00",
"entrada2": "13:00",
"saida2": "18:00",
"entrada3": null,
"saida3": null,
"flexivel": false,
"ehFolga": false
},
{
"diaDaSemana": "Ter",
"entrada1": "08:00",
"saida1": "12:00",
"entrada2": "13:00",
"saida2": "18:00",
"entrada3": null,
"saida3": null,
"flexivel": false,
"ehFolga": false
},
{
"diaDaSemana": "Qua",
"entrada1": "08:00",
"saida1": "12:00",
"entrada2": "13:00",
"saida2": "18:00",
"entrada3": null,
"saida3": null,
"flexivel": false,
"ehFolga": false
},
{
"diaDaSemana": "Qui",
"entrada1": "08:00",
"saida1": "12:00",
"entrada2": "13:00",
"saida2": "18:00",
"entrada3": null,
"saida3": null,
"flexivel": false,
"ehFolga": false
},
{
"diaDaSemana": "Sex",
"entrada1": "08:00",
"saida1": "12:00",
"entrada2": "13:00",
"saida2": "17:00",
"entrada3": null,
"saida3": null,
"flexivel": false,
"ehFolga": false
},
{
"diaDaSemana": "Sáb",
"entrada1": null,
"saida1": null,
"entrada2": null,
"saida2": null,
"entrada3": null,
"saida3": null,
"flexivel": false,
"ehFolga": true
},
{
"diaDaSemana": "Dom",
"entrada1": null,
"saida1": null,
"entrada2": null,
"saida2": null,
"entrada3": null,
"saida3": null,
"flexivel": false,
"ehFolga": true
}
]
}

  • Edit your reply and add the JSON file to it, this space here is for replies only. You can edit by here

Browser other questions tagged

You are not signed in. Login or sign up in order to post.