大家好,我是Flutter的新手,我试图将api数据提取到listview中,得到以下错误类型'String'不是'index'的'int'类型的子类型,'String'类型是不是“ int”类型的子类型,并且我没有从此代码中获取值。这里的问题是这里没有显示数据。
class MyClient extends MyClientService {
final int id, clientId, acceptedBy, pincode;
final double latitude, longitude;
final String perpose,
details,
when,
name,
mobile,
email,
orgName,
address,
city,
state,
country,
clientIp,
device,
createdAt,
updatedAt;
MyClient({
...
});
factory MyClient.fromJson(Map<String, dynamic> json) {
return MyClient(
...
);
}
Future<List<MyClient>> fetchClients() async {
var datas = await super.fetchDatas();
List dataList = json.decode(datas);
print(dataList); // Data Getting here
List<MyClient> clients =
dataList.map((req) => MyClient.fromJson(req)).toList();
print(clients); // Here nothing
return clients;
}
}
答案 0 :(得分:0)
这里缺少对象类型。这意味着某些数据以字符串形式来自API。但这就是int声明。显示问题在于数据不匹配。
import 'dart:convert';
import 'package:PandeetApp/services/myclient.dart';
class MyClient extends MyClientService {
final int id, clientId, acceptedBy, pincode;
final double latitude, longitude;
final String perpose,
details,
when,
name,
mobile,
email,
orgName,
address,
city,
state,
country,
clientIp,
device,
createdAt,
updatedAt;
MyClient({
this.id,
this.clientId,
this.perpose,
this.details,
this.when,
this.acceptedBy,
this.name,
this.mobile,
this.email,
this.orgName,
this.address,
this.city,
this.state,
this.country,
this.latitude,
this.longitude,
this.pincode,
this.clientIp,
this.device,
this.createdAt,
this.updatedAt,
});
factory MyClient.fromJson(Map<String, dynamic> json) {
return MyClient(
id: json['id'],
clientId: json['client_id'],
perpose: json['perpose'],
details: json['details'],
when: json['when'],
acceptedBy: json['accepted_by'],
name: json['name'],
mobile: json['mobile'],
email: json['email'],
orgName: json['org_name'],
address: json['address'],
city: json['city'],
state: json['state'],
pincode: json['pincode'],
country: json['country'],
latitude: json['latitude'],
longitude: json['longitude'],
clientIp: json['client_ip'],
device: json['device'],
createdAt: json['created_at'],
updatedAt: json['updated_at'],
);
}
Future<List<MyClient>> fetchClients() async {
var datas = await super.fetchDatas();
List dataList = json.decode(datas);
List<MyClient> clients =
dataList.map((req) => MyClient.fromJson(req)).toList();
return clients;
}
}
代替
import 'dart:convert';
import 'package:PandeetApp/services/myclient.dart';
class MyClient extends MyClientService {
final int id, clientId, acceptedBy;
final double latitude, longitude;
final String perpose,
details,
when,
name,
mobile,
email,
orgName,
address,
city,
state,
pincode,
country,
clientIp,
device,
createdAt,
updatedAt;
MyClient({
this.id,
this.clientId,
this.perpose,
this.details,
this.when,
this.acceptedBy,
this.name,
this.mobile,
this.email,
this.orgName,
this.address,
this.city,
this.state,
this.country,
this.latitude,
this.longitude,
this.pincode,
this.clientIp,
this.device,
this.createdAt,
this.updatedAt,
});
factory MyClient.fromJson(Map<String, dynamic> json) {
return MyClient(
id: json['id'],
clientId: json['client_id'],
perpose: json['perpose'],
details: json['details'],
when: json['when'],
acceptedBy: json['accepted_by'],
name: json['name'],
mobile: json['mobile'],
email: json['email'],
orgName: json['org_name'],
address: json['address'],
city: json['city'],
state: json['state'],
pincode: json['pincode'],
country: json['country'],
latitude: json['latitude'],
longitude: json['longitude'],
clientIp: json['client_ip'],
device: json['device'],
createdAt: json['created_at'],
updatedAt: json['updated_at'],
);
}
Future<List<MyClient>> fetchClients() async {
var datas = await super.fetchDatas();
List dataList = json.decode(datas);
List<MyClient> clients =
dataList.map((req) => MyClient.fromJson(req)).toList();
return clients;
}
}
答案 1 :(得分:0)
问题是变量类型不匹配,因此您可以继续进行操作的一种方法是在工厂方法中将所需类型的字符串数据解析为模态类中声明为非字符串的所有变量
factory MyClient.fromJson(Map<String, dynamic> json) {
return MyClient(
id: int.parse(json['id']),
clientId: int.parse(json['client_id']),
longitude: double.parse(json['longitude']),
);
}