英文:
How do I solve this case of "type 'String' is not a subtype of type 'int'" and type 'int' is not a subtype of type 'String'
问题
我的问题是当程序尝试执行ListView时,它会给我报错。当我将其定义为String时,它也会给出相同的错误,但是相反。这就是为什么到处都有日志的主要原因。以下是代码:
模型:
// 要解析此JSON数据,请执行以下操作
//
// final facturasDeVenta = facturasDeVentaFromJson(jsonString);
import 'dart:convert';
FacturasDeVenta facturasDeVentaFromJson(String str) =>
FacturasDeVenta.fromJson(json.decode(str));
String facturasDeVentaToJson(FacturasDeVenta data) =>
json.encode(data.toJson());
class FacturasDeVenta {
int count;
int totalCount;
List<VtaPedG> vtaPedGs;
FacturasDeVenta({
required this.count,
required this.totalCount,
required this.vtaPedGs,
});
factory FacturasDeVenta.fromJson(Map<String, dynamic> json) =>
FacturasDeVenta(
count: json["count"],
totalCount: json["total_count"],
vtaPedGs: List<VtaPedG>.from(
json["vta_ped_g"].map((x) => VtaPedG.fromJson(x))),
);
Map<String, dynamic> toJson() => {
"count": count,
"total_count": totalCount,
"vtaPedGs": List<dynamic>.from(vtaPedGs.map((x) => x.toJson())),
};
}
class VtaPedG {
int id;
int clt;
int emp;
VtaPedG({
required this.id,
required this.clt,
required this.emp,
});
factory VtaPedG.fromJson(Map<String, dynamic> json) => VtaPedG(
id: int.parse(json["id"]),
clt: int.parse(json["clt"]),
emp: int.parse(json["emp"]),
);
Map<String, dynamic> toJson() => {
"id": id,
"clt": clt,
"emp": emp,
};
}
显示部分:
import 'dart:convert';
import 'dart:developer';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import "package:velneoapp/api/api_model.dart";
class PartesView extends StatefulWidget {
const PartesView({super.key});
@override
State<PartesView> createState() => _PartesViewState();
}
class _PartesViewState extends State<PartesView> {
bool _isLoaded = true;
@override
void initState() {
super.initState();
_getData();
}
@override
void dispose() {
super.dispose();
_getData();
}
FacturasDeVenta? dataFromAPI;
_getData() async {
try {
String url =
"https://demoapi.velneo.com/verp-api/vERP_2_dat_dat/v1/vta_ped_g?page%5Bsize%5D=20&fields=id,clt,emp&api_key=api123";
http.Response res = await http.get(Uri.parse(url));
log("${res.statusCode}");
if (res.statusCode == 200) {
print("jiji");
dataFromAPI = await FacturasDeVenta.fromJson(json.decode(res.body));
_isLoaded = false;
setState(() {});
} else {
throw ("NONOAAAAAAA");
}
} catch (e) {
print("NONO");
log(e.toString());
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Partes"),
),
body: _isLoaded
? const Center(
child: CircularProgressIndicator(),
)
: ListView.builder(
scrollDirection: Axis.vertical,
itemCount: 10,
itemBuilder: (context, index) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
children: [
Text("\$${dataFromAPI!.vtaPedGs[index].id.toString()}"),
Text("\$${dataFromAPI!.vtaPedGs[index].clt.toString()}"),
],
),
);
},
),
);
}
}
我尝试搜索答案,但每种情况都与我的不同。这是我第一次编写一些Rest API代码,所以可能是原因。
如果有人能告诉我原因,因为我找不到需要调整代码的答案。
英文:
My problem is that when the program tries to do the ListView, it gives me that error. And when i define it as String, it gives the same error, but backwards. That's the main reason why there are logs everywhere. Here is the code:
Model:
// To parse this JSON data, do
//
// final facturasDeVenta = facturasDeVentaFromJson(jsonString);
import 'dart:convert';
FacturasDeVenta facturasDeVentaFromJson(String str) =>
FacturasDeVenta.fromJson(json.decode(str));
String facturasDeVentaToJson(FacturasDeVenta data) =>
json.encode(data.toJson());
class FacturasDeVenta {
int count;
int totalCount;
List<VtaPedG> vtaPedGs;
FacturasDeVenta({
required this.count,
required this.totalCount,
required this.vtaPedGs,
});
factory FacturasDeVenta.fromJson(Map<String, dynamic> json) =>
FacturasDeVenta(
count: json["count"],
totalCount: json["total_count"],
vtaPedGs: List<VtaPedG>.from(
json["vta_ped_g"].map((x) => VtaPedG.fromJson(x))),
);
Map<String, dynamic> toJson() => {
"count": count,
"total_count": totalCount,
"vtaPedGs": List<dynamic>.from(vtaPedGs.map((x) => x.toJson())),
};
}
class VtaPedG {
int id;
int clt;
int emp;
VtaPedG({
required this.id,
required this.clt,
required this.emp,
});
factory VtaPedG.fromJson(Map<String, dynamic> json) => VtaPedG(
id: int.parse(json["id"]),
clt: int.parse(json["clt"]),
emp: int.parse(json["emp"]),
);
Map<String, dynamic> toJson() => {
"id": id,
"clt": clt,
"emp": emp,
};
}
The part that shows it:
import 'dart:convert';
import 'dart:developer';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import "package:velneoapp/api/api_model.dart";
class PartesView extends StatefulWidget {
const PartesView({super.key});
@override
State<PartesView> createState() => _PartesViewState();
}
class _PartesViewState extends State<PartesView> {
bool _isLoaded = true;
@override
void initState() {
super.initState();
_getData();
}
@override
void dispose() {
super.dispose();
_getData();
}
FacturasDeVenta? dataFromAPI;
_getData() async {
try {
String url =
"https://demoapi.velneo.com/verp-api/vERP_2_dat_dat/v1/vta_ped_g?page%5Bsize%5D=20&fields=id,clt,emp&api_key=api123";
http.Response res = await http.get(Uri.parse(url));
log("${res.statusCode}");
if (res.statusCode == 200) {
print("jiji");
dataFromAPI = await FacturasDeVenta.fromJson(json.decode(res.body));
_isLoaded = false;
setState(() {});
} else {
throw ("NONOAAAAAAA");
}
} catch (e) {
print("NONO");
log(e.toString());
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Partes"),
),
body: _isLoaded
? const Center(
child: CircularProgressIndicator(),
)
: ListView.builder(
scrollDirection: Axis.vertical,
itemCount: 10,
itemBuilder: (context, index) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
children: [
Text("\$${dataFromAPI!.vtaPedGs[index].id.toString()}"),
Text("\$${dataFromAPI!.vtaPedGs[index].clt.toString()}"),
],
),
);
},
),
);
}
}
I tried searching for an answer, but every case is diferent than mine. It's my first time coding some Rest API code, so maybe that's the reason.
If anybody can please tell me why, because I can't find an answer that I have to adequate the code to.
答案1
得分: 1
我能看到在 API 响应中,你得到的数据如下:
"count": 20,
"total_count": 20071,
"vta_ped_g": [
{
"id": 1,
"clt": 4789,
"emp": "001"
}
]
而在你的模型中,你将 emp
解析为 int
。请将其类型更改为 String
,然后它应该可以正常工作。
class VtaPedG {
int id;
int clt;
String emp; // 从 int 更改为 String
VtaPedG({
required this.id,
required this.clt,
required this.emp,
});
factory VtaPedG.fromJson(Map<String, dynamic> json) => VtaPedG(
id: int.parse(json["id"]),
clt: int.parse(json["clt"]),
emp: json["emp"],
);
Map<String, dynamic> toJson() => {
"id": id,
"clt": clt,
"emp": emp,
};
}
英文:
I can see that in the api response, you are getting data as below
"count": 20,
"total_count": 20071,
"vta_ped_g": [
{
"id": 1,
"clt": 4789,
"emp": "001"
},
]
And in your model, you are parsing the emp
as int
. Change the type to String
and it should work.
class VtaPedG {
int id;
int clt;
String emp; <--- Changed this from int to String
VtaPedG({
required this.id,
required this.clt,
required this.emp,
});
factory VtaPedG.fromJson(Map<String, dynamic> json) => VtaPedG(
id: int.parse(json["id"]),
clt: int.parse(json["clt"]),
emp: json["emp"],
);
Map<String, dynamic> toJson() => {
"id": id,
"clt": clt,
"emp": emp,
};
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论