如何解决错误“_Map<String, dynamic>”不是“List<dynamic>”类型的子类型?在类型转换中

问题描述 投票:0回答:1

我想通过 id 获取,但出现此错误。 在 MarsatmasResponseModel 中,当您引入带有 List 类型 id 的数据时,会返回 Marsatmas。我收到此错误。

Parse Error: type '_Map<String, dynamic>' is not a subtype of type 'List<dynamic>?' in type cast - response body: {data: {id: 1,  code: adc, date: 2023-04-29T00:00:00}}
我的 getById 方法是这样的。

 Future<MarsatMas?> getbyId({required int id}) async {
   MarsatMas? _market;
   String query = "${NetworkRoutes.MARSATMASGETBYID.rawValue}?id=$id";

   final response =
       await manager.send<MarsatmasResponseModel, MarsatmasResponseModel>(
     query,
     parseModel: MarsatmasResponseModel(),
     method: RequestType.GET,
   );
   if (response != null) {
     if (response.data != null && response.data!.data != null) {
       _market =
           response.data!.data!.first;
       return _market;
     } else {
       return null;
     }
   } else {
     return null;
   }
 }

这是我的 MarsatmasResponseModel;

class MarsatmasResponseModel extends INetworkModel<MarsatmasResponseModel> {
  final List<MarsatMas>? data;
  final bool? success;
  final String? message;

  MarsatmasResponseModel({this.data, this.success, this.message});

  @override
  MarsatmasResponseModel fromJson(Map<String, dynamic> json) {
    return _$MarsatmasResponseModelFromJson(json);
  }
 @override
  Map<String, dynamic>? toJson() {
    return _$MarsatmasResponseModelToJson(this);
  }
  factory MarsatmasResponseModel.fromJson(Map<String, dynamic> json){
        return _$MarsatmasResponseModelFromJson(json);
  }
} 

ChatGbt 建议我这样做=>

 parseModel: (Map<String, dynamic>? json) =>  MarsatmasResponseModel.fromJson(json??{}),
当我像 ChatGbt 那样做时,我收到此错误
The argument type MarsatmasResponseModel Function(Map<String, dynamic>?) cant be assigned to the parameter type MarsatmasResponseModel.

json flutter dio jsonserializer
1个回答
0
投票

你可以看到这个。这个解决方案可以帮助您解决您的问题。

© www.soinside.com 2019 - 2024. All rights reserved.