Flutter 在颤振中使用异步构造函数
我想在工厂构造函数中解析一些东西,但一个属性将由future提供。不幸的是,flatter告诉我,不允许向工厂构造函数添加wait/async。有没有好办法解决这个问题 非常感谢你的帮助 这是我的代码:Flutter 在颤振中使用异步构造函数,flutter,async-await,factory,Flutter,Async Await,Factory,我想在工厂构造函数中解析一些东西,但一个属性将由future提供。不幸的是,flatter告诉我,不允许向工厂构造函数添加wait/async。有没有好办法解决这个问题 非常感谢你的帮助 这是我的代码: class ClothDetails{ final int clothId; final String title; final String image_path; bool pressed; ClothDetails( {this.clothId, thi
class ClothDetails{
final int clothId;
final String title;
final String image_path;
bool pressed;
ClothDetails(
{this.clothId, this.title, this.image_path, this.pressed}
);
factory ClothDetails.fromJson(Map<String, dynamic> aJson) {
return ClothDetails(
clothId: int.parse(aJson['id']),
title: aJson['title'],
image_path: aJson['image_path'],
pressed: isPressed(int.parse(aJson['id'])),
);
}
Future<bool> isPressed(int curId) async {
bool help;
final DatabaseHelper dbHelper = DatabaseHelper.instance;
List<Map<String, dynamic>> a = await dbHelper.queryRowsById(curId);
if(a.isEmpty){
help =false;
}else{
help= true;
}
return help;
}
}
类详细信息{
最终内衣领;
最后的字符串标题;
最终字符串图像路径;
布尔压榨;
衣服细节(
{this.clothId,this.title,this.image_路径,this.pressed}
);
factory ClothDetails.fromJson(映射aJson){
退货详情(
clothId:int.parse(aJson['id']),在,
标题:aJson[“标题”],
图像路径:aJson['image\u path'],
按下:isPressed(int.parse(aJson['id']),
);
}
未来isPressed(int-curId)异步{
bool-help;
final DatabaseHelper dbHelper=DatabaseHelper.instance;
列表a=await dbHelper.queryRowsById(curId);
如果(a.isEmpty){
帮助=错误;
}否则{
帮助=真实;
}
回报帮助;
}
}
你不能这样做
因为承包商只传递类的实例ClothDetails
您不能返回未来
,因为它不受支持
但是您可以在类中创建一个方法,它是Future
我希望你有这个想法构造函数({}){}你试过这个吗。