如何处理类型'List< dynamic>'不是类型'Map&lt;String,dynamic&gt;异常 Flutter 的子类型

knsnq2tg  于 2023-11-21  发布在  Flutter
关注(0)|答案(2)|浏览(129)

我试图显示来自API的信息列表。我已经分别创建了一个API和模型和提供程序类。当我试图访问API时,我得到了以下信息:
“List”不是类型“Map<String,dynamic>”的子类型”
其中我的API响应如下所示

[
    {
        "id": 1,
        "username": "naveen",
        "email": "[email protected]",
        "scheduledDate": "15-09-2022",
        "remarks": "demo",
        "createdAt": "2022-09-14T23:57:09.344Z",
        "updatedAt": "2022-09-14T23:57:09.344Z"
    },
    {
        "id": 2,
        "username": "naveen",
        "email": "[email protected]",
        "scheduledDate": "16-09-2022",
        "remarks": "demo",
        "createdAt": "2022-09-14T23:57:17.756Z",
        "updatedAt": "2022-09-14T23:57:17.756Z"
    }
]

字符串
我的模特课

class ScheduleListModel {
  int? id;
  String? username;
  String? email;
  String? scheduledDate;
  String? remarks;
  String? createdAt;
  String? updatedAt;

  ScheduleListModel(
      {this.id,
      this.username,
      this.email,
      this.scheduledDate,
      this.remarks,
      this.createdAt,
      this.updatedAt});

  ScheduleListModel.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    username = json['username'];
    email = json['email'];
    scheduledDate = json['scheduledDate'];
    remarks = json['remarks'];
    createdAt = json['createdAt'];
    updatedAt = json['updatedAt'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['id'] = id;
    data['username'] = username;
    data['email'] = email;
    data['scheduledDate'] = scheduledDate;
    data['remarks'] = remarks;
    data['createdAt'] = createdAt;
    data['updatedAt'] = updatedAt;
    return data;
  }
}


提供程序类

class ScheduleListProvider extends ChangeNotifier {
  bool isBack = false;
  late ScheduleListModel scheduleListModel;
  ScheduleListModel get getScheduleListModel => scheduleListModel;
  Future<void> getAllScheduleListData() async {
    getAllSchedule().then((response) => {
          if (response!.statusCode == 200)
            {
              scheduleListModel =
                  ScheduleListModel.fromJson(json.decode(response.body)),
              notifyListeners(),
            }
        });
  }
}


这是我的API调用

Future<http.Response?> getAllSchedule() async {
  late SharedPreferences logindata;
  logindata = await SharedPreferences.getInstance();
  late String? token = logindata.getString('token');
  http.Response? response;
  try {
    response = await http.get(Uri.parse(Config.getAllScheduleAPI), headers: {
      HttpHeaders.contentTypeHeader: "application/json",
      'x-access-token': '$token',
    });
  } catch (e) {
    log(e.toString());
  }
  return response;
}


当我尝试执行API调用时,我收到异常:
[错误:flutter/runtime/dart_vm_initializer.cc(41)]未处理的异常:类型'List'不是类型'Map<String,dynamic>'的子类型
帮我解决这个问题。

7qhs6swi

7qhs6swi1#

您的API响应是List<ScheduleListModel>,而不是单个的ScheduleListModel。

ScheduleListModel.fromJson(json.decode(response.body)),

字符串
抛出此错误消息。根据您想要对列表执行的操作,您应该将getAllScheduleListData()更改为可以处理ScheduleListModel列表的内容。

wgx48brx

wgx48brx2#

试试这个:

class ScheduleListProvider extends ChangeNotifier {

bool isBack = false;
  List<ScheduleListModel> scheduleList = [];

  Future<void> getAllScheduleListData() async {
    getAllSchedule().then((response) => {
      if (response!.statusCode == 200) {
          json.decode(response.body).forEach((element){
            scheduleList.add(ScheduleListModel.fromJson(element));
          });
          notifyListeners(),
        }
    });
  }
}

字符串

相关问题