gpt4 book ai didi

dart - 如何在for循环中创建实例?

转载 作者:IT王子 更新时间:2023-10-29 06:57:30 25 4
gpt4 key购买 nike

我从 API 获取数据。存储数据非常慢,因为在我的 for 循环中插入数据。如何在 for 循环之前或在类级别创建它的实例?

storeWoDescription(String url,String token) async {
final response = await http.get(
'${url}/v1.0/WoDescription',
headers: {'Authorization': 'Bearer ${token}'},);
final jsonResponse = json.decode(response.body);
WoDescription model = WoDescription.fromJson(jsonResponse);
int length = model.data.length;

for(int i=0; i<length; i++) {
var data = DataWoDescription(
i: model.data[i].i,
d: model.data[i].d,
e: model.data[i].e,
w: model.data[i].w,
a: model.data[i].a,
r: model.data[i].r,
t: model.data[i].t,
du: model.data[i].du,
s: model.data[i].s,
ra: model.data[i].ra,
cul: model.data[i].cul,
);
await HelperDefCatMaster().insertWoDescription(data);
}
}

最佳答案

这应该是实例化数据库助手的更好方法。

var helper;

void main() async {
helper = await HelperDefCatMaster(); // instantiate it just once and use it everywhere.
}

storeWoDescription(String url, String token) async {

final response = await http.get(
'${url}/v1.0/WoDescription',
headers: {'Authorization': 'Bearer ${token}'},
);
final jsonResponse = json.decode(response.body);
WoDescription model = WoDescription.fromJson(jsonResponse);
int length = model.data.length;

for (int i = 0; i < length; i++) {
var data = DataWoDescription(
i: model.data[i].i,
d: model.data[i].d,
e: model.data[i].e,
w: model.data[i].w,
a: model.data[i].a,
r: model.data[i].r,
t: model.data[i].t,
du: model.data[i].du,
s: model.data[i].s,
ra: model.data[i].ra,
cul: model.data[i].cul,
);
await helper.insertWoDescription(data);
}
}

关于dart - 如何在for循环中创建实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55632792/

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