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.– rubStackOverflow
Okay. var respDados = await _loginServico.getDado(user);
 usuario = UsuarioModelo.fromJson(respDados);
– Adilson JR