skip to Main Content

I’m trying to return the items from this api https://api.maisdecristo.com/api/parents/mdcget00_parentkids/48984974812

but always the error:

Unhandled Exception: type ‘List’ is not a subtype of type ‘Map<String, dynamic>’

this my object and model:

  Future<ProdutoModel> getProduto() async {
    try {
      final response = await http.get(Uri.parse(
          "https://api.maisdecristo.com/api/parents/mdcget00_parentkids/48984974812"));
      var res = jsonDecode(response.body);
      print(res);
      _accountListModel = ProdutoModel.fromJson(res);
      var data = res['filhos'] as List;
      setState(() {
        _list =
            data.map<FilhoModel>((json) => FilhoModel.fromJson(json)).toList();
      });
      return _accountListModel;
    } catch (e) {
      rethrow;
    }
  }
class ProdutoModel {
  ProdutoModel({
    required this.phone,
    required this.desperson,
    required this.desemail,
    required this.filhos,
  });

  final String phone;
  final String desperson;
  final String desemail;
  final List<FilhoModel> filhos;

  factory ProdutoModel.fromJson(Map<String, dynamic> json) => ProdutoModel(
        phone: json["phone"],
        desperson: json["desperson"],
        desemail: json["desemail"],
        filhos: List<FilhoModel>.from(
            json["filhos"].map((x) => FilhoModel.fromJson(x))),
      );
}

class FilhoModel {
  FilhoModel({
    required this.age,
    required this.firstname,
    required this.lastname,
  });

  final int age;
  final String firstname;
  final String lastname;

  factory FilhoModel.fromJson(Map<String, dynamic> json) => FilhoModel(
        age: json["age"],
        firstname: json["firstname"],
        lastname: json["lastname"],
      );
}

return this api

3

Answers


  1. You have type conversion issue make sure the types from the api is the same as the model

    Login or Signup to reply.
  2. you can use this website : https://app.quicktype.io
    to generate model to any json file without get any erorr and it gets you functions from json and to json

    Login or Signup to reply.
  3. The returned is at List, so you have to do something like:

    import 'dart:convert';
    
    import 'package:http/http.dart' as http;
    
    Future<void> main() async {
      final response = await http.get(Uri.parse(
          "https://api.maisdecristo.com/api/parents/mdcget00_parentkids/48984974812"));
      var res = jsonDecode(response.body);
      print(res);
      var list = res as List;
      for (var item in list) {
        var _accountListModel = ProdutoModel.fromJson(item); // model per item
        print(_accountListModel.phone);
        var data = item['filhos'] as List;
        var _list =
            data.map<FilhoModel>((json) => FilhoModel.fromJson(json)).toList();
        print(_list);
      }
    }
    
    class ProdutoModel {
      ProdutoModel({
        required this.phone,
        required this.desperson,
        required this.desemail,
        required this.filhos,
      });
    
      final String phone;
      final String desperson;
      final String desemail;
      final List<FilhoModel> filhos;
    
      factory ProdutoModel.fromJson(Map<String, dynamic> json) => ProdutoModel(
            phone: json["phone"],
            desperson: json["desperson"],
            desemail: json["desemail"],
            filhos: List<FilhoModel>.from(
                json["filhos"].map((x) => FilhoModel.fromJson(x))),
          );
    }
    
    class FilhoModel {
      FilhoModel({
        required this.age,
        required this.firstname,
        required this.lastname,
      });
    
      final int age;
      final String firstname;
      final String lastname;
    
      factory FilhoModel.fromJson(Map<String, dynamic> json) => FilhoModel(
            age: json["age"],
            firstname: json["firstname"],
            lastname: json["lastname"],
          );
    }
    
    [
      {
        "phone": "48984974812",
        "desperson": "Usuario admin",
        "desemail": "[email protected]",
        "filhos": [
          {
            "age": 7,
            "firstname": "Lorenzo",
            "lastname": "Chaves"
          },
          {
            "age": 14,
            "firstname": "teste",
            "lastname": "acompanhante"
          },
          {
            "age": 14,
            "firstname": "meu",
            "lastname": "filho"
          },
          {
            "age": 21,
            "firstname": "teste",
            "lastname": "teste"
          }
        ]
      }
    ]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search