【发布时间】:2020-07-28 23:11:27
【问题描述】:
我有一个小部件,其中一个属性是通过对数据库的查询获得的。所以这个函数必须被标记为异步的,但我不能在构造函数中调用它。有人知道如何解决这个问题吗?
class PositionWidget extends StatefulWidget {
final String streetName;
String date;
List<Map> notification;
final VoidCallback onDelete;
PositionWidget({Key key, this.streetName, this.date,this.notification, @required this.onDelete}): super(key: key){
this.notification = await DBHelper.instance.getNotification(this.streetName);
}
}
Future<List<Map>> getNotification(String street) async {
Database db = await DBHelper.instance.database;
var res = await db.query(table, columns: [columnNot], where: '${DBHelper.columnName} = ?', whereArgs: [street]);
return res;
}
【问题讨论】:
-
我认为您应该根据您从 db 获取的方法使用 Future 或 Stream 构建器。
标签: flutter asynchronous dart constructor