我想在工厂构造函数中解析一些东西,但是将来会给一个属性。不幸的是Flutter告诉我不允许将await / async添加到工厂构造函数中。有解决这个问题的好方法吗?
非常感谢您的帮助!
这是我的代码:
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;
}
}
您不能这样做
因为承包商仅传递了您的课程的实例ClothDetails
您不可以返回Future<ClothDetails>
,因为它不受支持
但是您可以在类中创建一个Future
的方法>
希望您有主意