简体   繁体   English

未处理的异常:“字符串”类型不是“地图”类型的子类型<string, dynamic> '</string,>

[英]Unhandled Exception: type 'String' is not a subtype of type 'Map<String, dynamic>'

I'm not exactly sure whats happened because my code was working fine but all of a sudden stopped working and I dont think I changed anything in relation to this.我不确定发生了什么,因为我的代码工作正常,但突然停止工作,我认为我没有改变任何与此相关的内容。 I've been stuck on this for two days and can't figure out what I'm doing wrong.我已经坚持了两天,无法弄清楚我做错了什么。 The data being returned from the API is correct and it returns with a status code of 200从 API 返回的数据是正确的,返回状态码为 200

I believe the error is something to do with the PTWorkoutPlanItem.fromJson() function.我相信该错误与PTWorkoutPlanItem.fromJson() function 有关。 I dont think it has anything to do with the List<AssignedUsers> or List<WorkoutPlanItem> models in the PTWorkoutPlanItem model as when I change these to dynamic , the error still occurs我认为这与 PTWorkoutPlanItem model 中的List<AssignedUsers>或 List< PTWorkoutPlanItem List<WorkoutPlanItem>模型有任何关系,因为当我将这些更改为dynamic时,错误仍然发生

The error says错误说

[ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: type 'String' is not a subtype of type 'Map<String, dynamic>'

code to get the data:获取数据的代码:

  getPlanData() async {
    var data = PTWorkoutPlanItem.fromJson(
        await get(url: 'pt/workouts/plan/' + planId.toString()));
  }

// Console logged the response here and it is correct, so this function works
Future<dynamic> get({url}) async {
  final response = await http.get(baseUrl + url, headers: headers);
  print(response.statusCode);

  dynamic res = json.decode(response.body);
  return res;
}

models楷模

class PTWorkoutPlanItem {
  int id;
  String title;
  List<AssignedUsers> assignedUsers;
  List<WorkoutPlanItem> workouts;
  int ptId;

  PTWorkoutPlanItem(
      {this.id, this.title, this.assignedUsers, this.workouts, this.ptId});

  factory PTWorkoutPlanItem.fromJson(Map<String, dynamic> json) {
    return PTWorkoutPlanItem(
      id: json['id'],
      title: json['title'],
      assignedUsers: List.from(json['assignedUsers'])
          .map((item) => AssignedUsers.fromJson(item))
          .toList(),
      workouts: List.from(json['workouts'])
          .map((item) => WorkoutPlanItem.fromJson(item))
          .toList(),
      ptId: json['ptId'],
    );
  }
}

class AssignedUsers {
  int id;
  String title;

  AssignedUsers({this.id, this.title});

  factory AssignedUsers.fromJson(Map<String, dynamic> json) {
    return AssignedUsers(id: json['id'], title: json['title']);
  }
}

class WorkoutPlanItem {
  int id;
  String title;
  int duration;
  int sets;
  int rest;

  WorkoutPlanItem({this.id, this.title, this.duration, this.sets, this.rest});

  factory WorkoutPlanItem.fromJson(Map<String, dynamic> json) {
    return WorkoutPlanItem(
      id: json['id'],
      title: json['title'],
      duration: json['duration'],
      sets: json['sets'],
      rest: json['rest'],
    );
  }
}

this is what gets retuned in the API这是在 API 中重新调整的内容

{
    "id": 1,
    "title": "Pull day",
    "assignedUsers": [
        {
            "id": 1,
            "title": "josh"
        },
        {
            "id": 2,
            "title": "marus"
        }
    ],
    "workouts": [
        {
            "id": 4,
            "title": "Workout item 4",
            "duration": 10,
            "sets": 3,
            "rest": 3
        },
        {
            "id": 1,
            "title": "Workout item 1",
            "duration": 10,
            "sets": 3,
            "rest": 3
        }
    ],
    "ptId": 1
}

Try this.尝试这个。 I have added a lot of type casts but the most important part is how I handle the List<dynamic> and cast each element to Map<String, dynamic> :我添加了很多类型转换,但最重要的部分是我如何处理List<dynamic>并将每个元素转换为Map<String, dynamic>

class PTWorkoutPlanItem {
  int id;
  String title;
  List<AssignedUsers> assignedUsers;
  List<WorkoutPlanItem> workouts;
  int ptId;

  PTWorkoutPlanItem(
      {this.id, this.title, this.assignedUsers, this.workouts, this.ptId});

  factory PTWorkoutPlanItem.fromJson(Map<String, dynamic> json) {
    return PTWorkoutPlanItem(
      id: json['id'] as int,
      title: json['title'] as String,
      assignedUsers: (json['assignedUsers'] as List<dynamic>)
          .cast<Map<String, dynamic>>()
          .map((item) => AssignedUsers.fromJson(item))
          .toList(),
      workouts: (json['workouts'] as List<dynamic>)
          .cast<Map<String, dynamic>>()
          .map((item) => WorkoutPlanItem.fromJson(item))
          .toList(),
      ptId: json['ptId'] as int,
    );
  }
}

class AssignedUsers {
  int id;
  String title;

  AssignedUsers({this.id, this.title});

  factory AssignedUsers.fromJson(Map<String, dynamic> json) {
    return AssignedUsers(id: json['id'] as int, title: json['title'] as String);
  }
}

class WorkoutPlanItem {
  int id;
  String title;
  int duration;
  int sets;
  int rest;

  WorkoutPlanItem({this.id, this.title, this.duration, this.sets, this.rest});

  factory WorkoutPlanItem.fromJson(Map<String, dynamic> json) {
    return WorkoutPlanItem(
      id: json['id'] as int,
      title: json['title'] as String,
      duration: json['duration'] as int,
      sets: json['sets'] as int,
      rest: json['rest'] as int,
    );
  }
}

Also, here is a Dart 2.12 compatible version where required as been added to the named parameters since they are all not allowed to be null and does not have any default value:此外,这是一个 Dart 2.12 兼容版本, required添加到命名参数中,因为它们都不允许是null并且没有任何默认值:

class PTWorkoutPlanItem {
  int id;
  String title;
  List<AssignedUsers> assignedUsers;
  List<WorkoutPlanItem> workouts;
  int ptId;

  PTWorkoutPlanItem(
      {required this.id,
      required this.title,
      required this.assignedUsers,
      required this.workouts,
      required this.ptId});

  factory PTWorkoutPlanItem.fromJson(Map<String, dynamic> json) {
    return PTWorkoutPlanItem(
      id: json['id'] as int,
      title: json['title'] as String,
      assignedUsers: (json['assignedUsers'] as List<dynamic>)
          .cast<Map<String, dynamic>>()
          .map((item) => AssignedUsers.fromJson(item))
          .toList(),
      workouts: (json['workouts'] as List<dynamic>)
          .cast<Map<String, dynamic>>()
          .map((item) => WorkoutPlanItem.fromJson(item))
          .toList(),
      ptId: json['ptId'] as int,
    );
  }
}

class AssignedUsers {
  int id;
  String title;

  AssignedUsers({required this.id, required this.title});

  factory AssignedUsers.fromJson(Map<String, dynamic> json) {
    return AssignedUsers(id: json['id'] as int, title: json['title'] as String);
  }
}

class WorkoutPlanItem {
  int id;
  String title;
  int duration;
  int sets;
  int rest;

  WorkoutPlanItem(
      {required this.id,
      required this.title,
      required this.duration,
      required this.sets,
      required this.rest});

  factory WorkoutPlanItem.fromJson(Map<String, dynamic> json) {
    return WorkoutPlanItem(
      id: json['id'] as int,
      title: json['title'] as String,
      duration: json['duration'] as int,
      sets: json['sets'] as int,
      rest: json['rest'] as int,
    );
  }
}

Update: Can you also try change your method into this:更新:您也可以尝试将您的方法更改为:

Future<Map<String, dynamic>> get({url}) async {
  final response = await http.get(baseUrl + url, headers: headers);
  print(response.statusCode);

  final res = json.decode(response.body) as Map<String, dynamic>;
  return res;
}

暂无
暂无

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

相关问题 未处理的异常:“字符串”类型不是“地图”类型的子类型<String, dynamic> &#39; 将 JSON 转换为 Map 时<String, dynamic>在颤振中 - Unhandled Exception: type 'String' is not a subtype of type 'Map<String, dynamic>' when converting JSON to a Map<String, dynamic> in Flutter 未处理的异常:类型 '_CompactLinkedHashSet<(dynamic) => Map<string, dynamic> >' 不是类型 '(Map<string, dynamic> ) => 'f' 的用户'</string,></string,> - Unhandled Exception: type '_CompactLinkedHashSet<(dynamic) => Map<String, dynamic>>' is not a subtype of type '(Map<String, dynamic>) => User' of 'f' E/Flutter 未处理异常:类型 _InternalLinkedHashMap<dynamic, dynamic> ' 不是类型 'Map 的子类型<string, string> ?</string,></dynamic,> - E/Flutter Unhandled Exception: type _InternalLinkedHashMap<dynamic, dynamic>' is not a subtype of type 'Map<String, String>? 在 dart 中解析对象(未处理的异常:类型 &#39;_InternalLinkedHashMap<dynamic, dynamic> &#39; 不是类型 &#39;Map 的子类型<String, dynamic> &#39;) - Parsing object in dart (Unhandled Exception: type '_InternalLinkedHashMap<dynamic, dynamic>' is not a subtype of type 'Map<String, dynamic>') 未处理的异常:类型 '_InternalLinkedHashMap<string, dynamic> ' 不是类型 'String' 的子类型</string,> - Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'String' 未处理的异常:键入“列表”<dynamic> &#39; 不是类型 &#39;Map 的子类型<String, dynamic> &#39; 在颤动中如何像这样发布 json obj - Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>' in flutter how to post json obj like this 未处理的异常:键入“列表”<dynamic> ' 不是类型 'Map 的子类型<string, dynamic> ' 在飞镖/ flutter</string,></dynamic> - Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>' in dart/ flutter 未处理的异常:键入“列表<dynamic> ' 不是 'Map 类型的子类型<string, dynamic> ' flutter 错误</string,></dynamic> - Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>' flutter error 未处理的异常:类型 '_InternalLinkedHashMap<string, dynamic> ' 不是类型 'Response' 的子类型</string,> - Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Response' 未处理的异常:“String”类型不是“List”类型的子类型<dynamic> &#39; - Unhandled Exception: type 'String' is not a subtype of type 'List<dynamic>'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM