[英]Error: Expected a value of type 'int', but got one of type 'String'; flutter
[英]flutter error : Expected a value of type 'int' but got one of type 'Null'
來自 .NET Core 6 API 的響應正文:
[{"establishmentId":1,"establishmentName":"Secret","addressId":1,"documentationId":1,"address":null,"documentation":null,"associationsEts":[],"prices":[]},{"establishmentId":2,"establishmentName":"HRB","addressId":2,"documentationId":2,"address":null,"documentation":null,"associationsEts":[],"prices":[]}]
我的 model class:
class Establishment {
final int id;
final String name;
final int addressid;
final int documentationid;
Establishment(
{required this.id,
required this.name,
required this.addressid,
required this.documentationid});
factory Establishment.fromJson(Map<String, dynamic> json) {
return Establishment(
id: json['id'],
name: json['name'],
addressid: json['addressid'],
documentationid: json['documantationid']);
}
}
問題是快照出錯,我希望快照接受 null 值,有人可以幫我解決這個問題嗎? 謝謝
更新:
json 沒有出現問題,因為我修改了它們,問題仍然存在
class Establishment {
final int establishmentId;
final String establishmentName;
final int addressId;
final int documentationId;
Establishment(
{required this.establishmentId,
required this.establishmentName,
required this.addressId,
required this.documentationId});
factory Establishment.fromJson(Map<String, dynamic> json) {
return Establishment(
establishmentId: json['establishmentId'],
establishmentName: json['establishmentName'],
addressId: json['addressId'],
documentationId: json['documantationId']);
}
}
您的“fromJson”解析中出現錯誤,將您的工廠 fromJson function 替換為:
factory Establishment.fromJson(Map<String, dynamic> json) {
return Establishment(
id: json['establishmentId'],
name: json['establishmentName'],
addressid: json['addressId'],
documentationid: json['documentationId']);
}
提示:檢查 camelCase 名稱,在您的解析中名稱必須相等。
此代碼仍然無法正常工作“空值而不是 int”
class Establishment {
final int establishmetnId;
final String establishmetnName;
final int addressId;
final int documentationId;
Establishment(
{required this.establishmetnId,
required this.establishmetnName,
required this.addressId,
required this.documentationId});
factory Establishment.fromJson(Map<String, dynamic> json) {
return Establishment(
establishmetnId: json['establishmentId'],
establishmetnName: json['establishmentName'],
addressId: json['addressId'],
documentationId: json['documantationId']);
}
}
Future<Establishment> fetchEstablishment() async {
final response =
await http.get(Uri.parse('https://localhost:7282/api/Establishmens'));
debugPrint(response.statusCode.toString());
debugPrint(response.body);
if (response.statusCode == 200) {
return Establishment.fromJson(jsonDecode(response.body));
} else {
throw Exception('Failed to load Establishments');
}
}
List<Establishment> parseEstablishment(String responseBody) {
final parsed = jsonDecode(responseBody).cast<Map<String, dynamic>>();
return parsed
.map<Establishment>((json) => Establishment.fromJson(json))
.toList();
}
Future<List<Establishment>> fetchListEstablishments() async {
final response =
await http.get(Uri.parse('https://localhost:7282/api/Establishments'));
debugPrint(response.statusCode.toString());
debugPrint(response.body);
if (response.statusCode == 200) {
return compute(parseEstablishment,response.body);
} else {
throw Exception('Failed to load establishment');
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.