例外:'List<dynamic>' 型不是 'Map<String, dynamic>' 型的子类型



下面是我陷入困境的代码。它抛出了一个异常,我无法解决它。

import 'package:attendance_worklog/Events/eventSchedule.dart';
import 'package:attendance_worklog/login/loginModel.dart';
import 'package:http/http.dart' as http;
import 'eventScheduleModel.dart';
class EventScheduleService extends EventSchedule{
LoginModel? data;
EventScheduleService({required this.data}) : super(data: data);
var url = Uri.parse('http://10.0.2.2:5001/naf-vip-server/us-central1/api/meets/');
Future<List<EventScheduleModel>?> getSchedule() async{
try{
print("In Try");
final response = await http.get(url,headers: {'Authorization': 'Bearer ${data!.token}'});
if (response.statusCode == 200){
print("In If");
final List<EventScheduleModel> eventScheduleModel = eventScheduleModelFromJson(response.body) as List<EventScheduleModel>;
print(eventScheduleModel);
return eventScheduleModel;
}else{
print("0");
return <EventScheduleModel>[];
}
}catch (e){
print("0 excep");
print(e);
return <EventScheduleModel>[];
}
}
}

当调用函数时,输出如下:

I/flutter (18763): In Try
I/flutter (18763): In If
I/flutter (18763): 0 excep
I/flutter (18763): type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>'

这意味着CCD_ 1有一些问题。

将异常作为type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>'抛出

如果有人知道解决方案,请帮忙。

response.body上使用map,因为response.bodyMap<String, dynamic>List

final List<EventScheduleModel> eventScheduleModel = (response.body as List)
.map((e) => eventScheduleModelFromJson(e))
.toList();

相关内容

最新更新