Uncaught (in promise) Error: Expected a value of type 'List<dynamic>', but got one of type '_JsonMap'

Uncaught (in promise) Error: Expected a value of type 'List<dynamic>', but got one of type '_JsonMap'

我是 Flutter 新手。我正在尝试从我的 UI 中使用 Rest_Api 来发出获取请求,但是我收到错误 "Expected a value of type 'List', but got one of键入'_JsonMap'",它已尝试根据网上找到的建议进行调试,但我似乎无法解决此问题。这是我收到的错误的屏幕截图:

这是我的 模型服务 类:

import 'package:json_annotation/json_annotation.dart';
part 'unit.g.dart';

@JsonSerializable(explicitToJson: true)
class Unit {
  String ? id;
  String name;
  String baseUnit;
  var baseValue;

  Unit({
    this.id,
    required this.name,
    required this.baseUnit,
    required this.baseValue,
  });
  factory Unit.fromJson(Map < String, dynamic > json) => _$UnitFromJson(json);
  Map < String, dynamic > toJson() => _$UnitToJson(this);
}

Future < List < Unit >> search(searchText, page) async {
  var searchUrl = "${ApiEndPoint.baseUrl}/unit?q=$searchText&page=$page";
  var response = await http.get(
    Uri.parse(searchUrl),
    headers: {
      "Content-Type": "application/json"
    },
  );
  var units = convert.json.decode(response.body) as List;
  List < Unit > unitList = [];
  unitList = units.map((i) => Unit.fromJson(i)).toList();
  // print(unitList.length);
  // print(response.body);
  return unitList;
}
序列化模型 Class 是;

// GENERATED CODE - DO NOT MODIFY BY HAND

part of 'unit.dart';

// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************

Unit _$UnitFromJson(Map < String, dynamic > json) {
  return Unit(
    id: json['id'] as String ? ,
    name : json['name'] as String,
    baseUnit: json['baseUnit'] as String,
    baseValue: json['baseValue'],
  );
}

Map < String, dynamic > _$UnitToJson(Unit instance) => < String, dynamic > {
  'id': instance.id,
  'name': instance.name,
  'baseUnit': instance.baseUnit,
  'baseValue': instance.baseValue,
};

我哪里做错了?

var units = convert.json.decode(response.body) as List;

您的回复 returns JSON 对象,但您正在投射为 List。 您的回复是:

{
  "page": 1,
  "pages": 1,
  "pageSize": 20,
  "totalRows": 4,
  "units": [
    {
      "id": "fed0b37c-4f11-4169-9e53-c5553e106936",
      "name": "m²",
      "baseUnit": "m²",
      "baseValue": 1
    },
    {
      "id": "d896b847-7cbf-48e5-963b-0e7d79a51f40",
      "name": "ft²",
      "baseUnit": "m²",
      "baseValue": 0.092903
    },
    {
      "id": "04b55ad0-5367-49ce-850f-2ab6fe184bc6",
      "name": "feet",
      "baseUnit": "ft",
      "baseValue": 0.536127
    },
    {
      "id": "684656a6-6e7d-4b0d-af5e-c7c1247ac9af",
      "name": "inche",
      "baseUnit": "in",
      "baseValue": 1.536127
    }
  ]
}

我猜您正试图从响应中获取单位列表。如果是这样试试这个:

var units = (convert.json.decode(response.body) as Map<String,dynamic>)["units"];

我不得不更改以将服务中的 return 值更改为此,并且它以某种方式工作。

Future < List < UnitSearch >> search(searchText, page) async {
  var searchUrl = "${ApiEndPoint.baseUrl}/unit?q=$searchText&page=$page";
  http.Response response = await http.get(
    Uri.parse(searchUrl),
    headers: {
      "Content-Type": "application/json"
    },
  );
  final data = json.decode(response.body);
  return List < UnitSearch > .from(
    data['units'].map((item) => UnitSearch.fromJson(item)));
}