Flutter:“type 'Teams' is not a subtype of type 'int' in type cast”错误来自请求
Flutter : " type 'Teams' is not a subtype of type 'int' in type cast " error comes from request
我在 Flutter 中发送请求有问题,我有这个模型:
import 'dart:convert';
List<Teams> teamsFromJson(String str) =>
List<Teams>.from(json.decode(str).map((x) => Teams.fromJson(x)));
String teamsToJson(List<Teams> data) =>
json.encode(List<dynamic>.from(data.map((x) => x.toJson())));
class Teams {
Teams({
this.club,
this.price,
this.surename,
this.id,
this.league,
});
final club;
final price;
final surename;
final id;
final league;
factory Teams.fromJson(Map<String, dynamic> json) => Teams(
club: json["club"],
price: json["price"],
surename: json["surename"],
id: json["id"],
league: json["league"],
);
Map<String, dynamic> toJson() => {
"club": club,
"price": price,
"surename": surename,
"id": id,
"league": league,
};
}
我添加初始值并在提供程序中更新它们:
List<Teams> get teams => _teams;
List<Teams> _teams = [
Teams(club: "", price: 0, surename: "", id: "", league: ""),
Teams(club: "", price: 0, surename: "", id: "", league: ""),]
addToTeam(data, index) {
teams[index]=Team(club: data.club,
price: data.price,
surename: data.surname,
id: data.id,
league: data.leagueName);
}
它工作正常,现在我想作为请求发送列表团队,我添加按钮并创建这样的方法:
onPressed: () {
ApiService().saveTeam(teamsProvider.teams);
}
在 ApiService 上我有这个请求:
class ApiService {
var url = 'http://10.0.2.2:8000/api/v1';
Future saveTeam(data) async {
var newurl = Uri.parse(url + '/send_test');
try {
var response = await http.post(newurl, body: data);
var result = jsonDecode(response.body);
print(result);
} catch (e) {
print('error : $e');
}
}
}
api 请求就是 return laravel 中的请求:
public function send_test(Request $request)
{
return $request;
}
结果我得到这个错误信息:type 'Teams' is not a subtype of type 'int' in type cast
我该如何解决这个问题?
我自己解决了,我将Team列表转换成String并用json解码:
class ApiService {
var url = 'http://10.0.2.2:8000/api/v1';
Future saveTeam(List<Teams> data) async {
var list = [];
data.map((e) {
list.add({
"club": e.club,
"price": e.price,
"surename": e.surename,
"id": e.id,
"league": e.league
});
}).toList();
try {
var newurl = Uri.parse(url + '/send_test');
var response = await http.post(newurl, body: jsonEncode(list));
var result = jsonDecode(response.body);
print(result);
} catch (e) {
print('error : $e');
}
}
}
然后在 api 在 laaravel/lumen 收到 json 并再次解码 :
public function send_test(Request $request)
{
$result = json_decode($request->getContent(), true);
return $result;
}
我在 Flutter 中发送请求有问题,我有这个模型:
import 'dart:convert';
List<Teams> teamsFromJson(String str) =>
List<Teams>.from(json.decode(str).map((x) => Teams.fromJson(x)));
String teamsToJson(List<Teams> data) =>
json.encode(List<dynamic>.from(data.map((x) => x.toJson())));
class Teams {
Teams({
this.club,
this.price,
this.surename,
this.id,
this.league,
});
final club;
final price;
final surename;
final id;
final league;
factory Teams.fromJson(Map<String, dynamic> json) => Teams(
club: json["club"],
price: json["price"],
surename: json["surename"],
id: json["id"],
league: json["league"],
);
Map<String, dynamic> toJson() => {
"club": club,
"price": price,
"surename": surename,
"id": id,
"league": league,
};
}
我添加初始值并在提供程序中更新它们:
List<Teams> get teams => _teams;
List<Teams> _teams = [
Teams(club: "", price: 0, surename: "", id: "", league: ""),
Teams(club: "", price: 0, surename: "", id: "", league: ""),]
addToTeam(data, index) {
teams[index]=Team(club: data.club,
price: data.price,
surename: data.surname,
id: data.id,
league: data.leagueName);
}
它工作正常,现在我想作为请求发送列表团队,我添加按钮并创建这样的方法:
onPressed: () {
ApiService().saveTeam(teamsProvider.teams);
}
在 ApiService 上我有这个请求:
class ApiService {
var url = 'http://10.0.2.2:8000/api/v1';
Future saveTeam(data) async {
var newurl = Uri.parse(url + '/send_test');
try {
var response = await http.post(newurl, body: data);
var result = jsonDecode(response.body);
print(result);
} catch (e) {
print('error : $e');
}
}
}
api 请求就是 return laravel 中的请求:
public function send_test(Request $request)
{
return $request;
}
结果我得到这个错误信息:type 'Teams' is not a subtype of type 'int' in type cast
我该如何解决这个问题?
我自己解决了,我将Team列表转换成String并用json解码:
class ApiService {
var url = 'http://10.0.2.2:8000/api/v1';
Future saveTeam(List<Teams> data) async {
var list = [];
data.map((e) {
list.add({
"club": e.club,
"price": e.price,
"surename": e.surename,
"id": e.id,
"league": e.league
});
}).toList();
try {
var newurl = Uri.parse(url + '/send_test');
var response = await http.post(newurl, body: jsonEncode(list));
var result = jsonDecode(response.body);
print(result);
} catch (e) {
print('error : $e');
}
}
}
然后在 api 在 laaravel/lumen 收到 json 并再次解码 :
public function send_test(Request $request)
{
$result = json_decode($request->getContent(), true);
return $result;
}