我有一个reward
模型。如下所示:
class RewardModel {
String id, status;
DateTime createdAt, updatedAt;
RewardModel({
@required this.id,
@required this.status,
@required this.createdAt,
@required this.updatedAt,
});
}
在提供方,我有以下代码:
try {
data['collection']['userData']['rewards'].forEach((result){
print(result);
_rewards.add(
RewardModel(
id: result['id'].toString(),
status: status(result['status_id']),
createdAt: DateTime.parse(result['created_at']),
updatedAt: DateTime.parse(result['updated_at']),
)
);
});
} catch (e) {
print(e);
}
我确实从请求中获取数据,如下所示:
flutter: {id: 1, user_id: 1, status_id: 4, created_at: 2019-12-23 20:54:43, updated_at: 2019-12-23 20:54:43}
flutter: type 'int' is not a subtype of type 'String'
但是在下一行,我得到了错误。怎么了?
以下答案对我也没有用:
flutter http post "type 'int' is not a subtype of type 'String' in type cast"
答案 0 :(得分:0)
当您将 int 值解析为 String 变量
时,会发生此错误只需更改此行
来自
status: status(result['status_id']),
至
status: status(result['status_id'].toString()),
或更改此行
来自
String status
至
int status