gpt4 book ai didi

flutter - flutter Api的Json

转载 作者:行者123 更新时间:2023-12-03 04:26:36 25 4
gpt4 key购买 nike

我收到API的这类回应

{
"success": 1,
"data": [
{
"id": 539,
"user_id": 3115,
"amount": 5788,
"payment_gateway": "bank",
"message": "chchhb",
"status": "waiting",
"source": "everyone",
"created_ts": "2019-12-19 13:41:17",
"processed_ts": null
},
]
}

这是我的模特
class Model {
final String status;
final List<String> data;

Model({
this.status,
this.data
});

factory Model.fromJson(Map<String, dynamic> parsedJson) {
var data = parsedJson['data'];
List<String> dataList = data.cast<String>();
return Model(
status: parsedJson['status'],
data: dataList
);
}

}

这就是我从模型中获取数据的方式
 Future<List<String>> getWithdrawals() async {
final userModel = Provider.of<UserModel>(context);
var userId = userModel.user.id;
Map<String, String> requestHeaders = {'Content-type': 'application/json'};
var body = {
'id': userId,
};
final response = await http.post(url);
if (response.statusCode == 200){
var jsonresponse = json.decode(response.body);
var withdrawals = WithdrawalModel.fromJson(jsonresponse);
return withdrawals.data;
} else {
print("Error" + response.body);
return null;
}
}

我无法在屏幕上显示数据这给我一个错误,例如
type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'String' in typecast

FutureBuilder<List>中,我无法获取数据
我不知道我在哪里做错了请帮助...

最佳答案

我认为您的响应类型当前不正确,它是List<String>,但是应该类似于List<Details>,并且您需要创建另一个名为Details的模型类(或者您可以更改名称)。
您只需将响应here放进去。它将为您生成 Dart 类模型。

注意:您必须从响应中删除多余的逗号才能使用模型生成器,然后您的响应才会像。

{
"success": 1,
"data": [
{
"id": 539,
"user_id": 3115,
"amount": 5788,
"payment_gateway": "bank",
"message": "chchhb",
"status": "waiting",
"source": "everyone",
"created_ts": "2019-12-19 13:41:17",
"processed_ts": null
}
]
}

编辑:

例如,在上述模型中,只需将List类型的字符串更改为Details(Model),反之亦然:
class Model {
final String status;
final List<Details> data; // Update string with model

}

关于flutter - flutter Api的Json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59421356/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com