flutter中如何设置json数据列表

db2dz4w8  于 2023-02-05  发布在  Flutter
关注(0)|答案(3)|浏览(81)

我正在尝试返回此api https://api.maisdecristo.com/api/parents/mdcget00_parentkids/48984974812中的项
但总是错误
未处理的异常:类型"List"不是类型"Map〈String,dynamic〉"的子类型
这是我的目标和模型:

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"],
      );
}

返回this api

wwodge7n

wwodge7n1#

返回的位于List,因此您必须执行以下操作:

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": "admin@hcode.com.br",
    "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"
      }
    ]
  }
]
cunj1qz1

cunj1qz12#

您有类型转换问题,请确保来自api的类型与模型相同

eufgjt7s

eufgjt7s3#

你可以使用这个网站:https://app.quicktype.io
生成任何json文件的模型,而不会出现任何错误,并且它可以从json和json获取函数

相关问题