简体   繁体   English

为什么我在尝试创建列表时收到 _TypeError?

[英]Why am I getting a _TypeError when trying to create a list?

I have the following JSON that is getting returned from an API call:我有以下 JSON 从 API 调用返回:

{
    "categories": {
       "mortgage": "Mortgage",
       "haircutsClothing": "Haircuts & Clothing",
       "homeRepairMaintenance": "Home Repair & Maintenance"
    },
    "other": {...}
}

And then I have this class acting as a model for the JSON data:然后我将这个 class 用作 JSON 数据的 model:

class APIData {
  final Map<String, dynamic> parsedJson;

  APIData({required this.parsedJson});

  factory APIData.fromJson(Map<String, dynamic> parsedJson) {
    List<Category> categories = parsedJson['categories']
        .map((i) => Category.fromJson(i))
        .toList();
    return APIData(parsedJson: parsedJson);
  }
}

class Category {
  final String key;
  final String category;

  Category({required this.key, required this.category});

  factory Category.fromJson(Map<String, dynamic> parsedJson) {
    return Category(key: parsedJson['key'], category: parsedJson['value']);
  }
}

When I run that, I get this error:当我运行它时,我收到此错误:

_TypeError (type '(dynamic) => Category' is not a subtype of type '(String, dynamic) => MapEntry<dynamic, dynamic>' of 'transform')

What am I doing wrong that is causing this error?我做错了什么导致此错误?

method.map on Map object has to return Map object too. method.map on Map object has to return Map object too.

try this尝试这个

final categoriesMap = parsedJson['categories'] as Map;

final List<Category> categories =
        categoriesMap.entries
        .map((e) => Category(key: e.key, category: e.value))
        .toList();

entries from a Map returns an Iterable of Map Entry.来自 Map 的条目返回 Map 条目的 Iterable。 then you can iterate through it and use key and value properties.然后您可以遍历它并使用键和值属性。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我的通用 StatefulWidget class 在运行时会出现 TypeError? - Why am I getting TypeError at runtime with my generic StatefulWidget class? 为什么在尝试使用 flutter chopper 和内置值调用方法时会出错? - Why am I getting an error when trying to call a method with flutter chopper and built value? Flutter:尝试启动时收到过时的消息 - Flutter: I am getting an outdated message when trying to launch 我正在尝试向我的应用程序添加身份验证,但这样做时出现以下映射错误: - I am trying to add authentication to my application, and I am getting the following mapping error when doing so: 我正在尝试获取要传递到屏幕的对象列表,但我不断收到类型转换错误 - I am trying to get a list of objects to pass to my screen, but I keep getting a type cast error 为什么我得到空值? - why i am getting a null? 我正在尝试使用从API获得的数据创建列表视图 - I am trying to create a list view with the data that I got from API 我正在尝试创建将数字作为输入的 TextFormFields 列表,我想对所有这些数字求和 - I am trying to create list of TextFormFields which takes numbers as inputs and I want to Sum all those numbers 我在颤振中收到一个调用空错误的吸气剂。 在尝试访问列表的长度时 - I am getting a getter called on null error in flutter. while trying to access the length of a list 谁能告诉我为什么我在尝试实现动画时会在 Flutter 中收到此错误 - Can anybody tell why am I getting this error in Flutter while trying to implement animation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM