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

Dart 如何在for循环中创建实例?,dart,flutter,Dart,Flutter,我从API获取数据。存储数据非常慢,因为在我的for循环中插入数据。如何在循环之前或在类级别创建其实例 storeWoDescription(String url,String token) async { final response = await http.get( '${url}/v1.0/WoDescription', headers: {'Authorization': 'Bearer ${token}'},); final jsonResponse = js

我从API获取数据。存储数据非常慢,因为在我的
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);
  }
}
storeWoDescription(字符串url、字符串令牌)异步{
最终响应=等待http.get(
“${url}/v1.0/WoDescription”,
头文件:{'Authorization':'Bearer${token}},});
final jsonResponse=json.decode(response.body);
WoDescription model=WoDescription.fromJson(jsonResponse);
int length=model.data.length;

对于(inti=0;i这应该是实例化数据库助手的更好方法

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);
  }
}
var助手;
void main()异步{
helper=wait HelperDefCatMaster();//只实例化一次,然后在任何地方使用它。
}
storeWoDescription(字符串url、字符串标记)异步{
最终响应=等待http.get(
“${url}/v1.0/WoDescription”,
标头:{'Authorization':'Bearer${token}},
);
final jsonResponse=json.decode(response.body);
WoDescription model=WoDescription.fromJson(jsonResponse);
int length=model.data.length;
for(int i=0;i
我在我的main方法中添加了helper,但是当使用
wait helper.insertWoDescription(data);
在函数helper中单击按钮后为空,我这样调用函数:
wait HelperDefCatMaster().deleteWoDescription();wait storeWoDescription(_url,tokens)
这是正确的方法吗?仍然很慢。为什么第一,助手肯定不应该是
null
。我不知道你是怎么做到的,你错过了什么。第二,我发布的解决方案是初始化数据库的一个很好的实践,所以不要使用第二个注释。不要在for循环中实例化它,不要使用
HelperDefCatMaster()
无处不在。它应该只是一次调用。我在哪里调用此函数?storeWoDescription()