“String”类型不是“int”类型的子类型
Posted
技术标签:
【中文标题】“String”类型不是“int”类型的子类型【英文标题】:type 'String' is not a subtype of type 'int' 【发布时间】:2020-12-14 20:28:12 【问题描述】:大家好,我是 Flutter 的新手,我正在尝试将 api 数据提取到 listview 中,得到以下错误类型 'String' is not a subtype of type 'int' of 'index' , type 'String' is不是“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;
【问题讨论】:
【参考方案1】:问题在于变量类型不匹配,因此您可以继续进行的一种方法是在工厂方法中将所需类型的字符串数据解析为所有在模态类中声明为非字符串的变量
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']),
);
【讨论】:
【参考方案2】:这里缺少对象类型。这意味着一些数据来自 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;
【讨论】:
以上是关于“String”类型不是“int”类型的子类型的主要内容,如果未能解决你的问题,请参考以下文章
ValueNotifier 未处理的异常:类型“String”不是“index”类型“int”的子类型
颤振错误:类型“int”不是类型转换中“String”类型的子类型
未处理的异常:'String' 类型不是'index' 的'int' 类型的子类型问题 Dart 和颤振
SetState Flutter 导致错误:“int”类型不是“String”类型的子类型
Flutter fromJson - 未处理的错误未处理的错误类型'String'不是'int'类型的子类型发生在实例中