如果我有一个ListView来显示来自sql的数据。1)这个关闭数据库的函数应该放在哪里? 2)_Close函数足以关闭数据库还是需要使用异步??
void _Close(Database db) {
db.close();
}这是我的Listview:
ListView getStudentsList() {
return ListView.builder(
itemCount: count,
itemBuilder: (BuildContext context, int position) {
return Card(
color: Colors.white,
elevation: 2.0,
child: ListTile(
leading: CircleAvatar(
backgroundColor: isPassed(this.studentsList[position].pass),
child: getIcon(this.studentsList[position].pass),
),
title: Text(this.studentsList[position].name),
subtitle: Text(this.studentsList[position].description + " | " +
this.studentsList[position].date),
trailing:
GestureDetector(
child: Icon(Icons.delete, color: Colors.grey,),
onTap: () {
_delete(context, this.studentsList[position]);
},
)
,
onTap: () {
navigateToStudent(this.studentsList[position], "Edit Student");
},
),
);
});
}发布于 2020-04-09 10:47:53
使用异步函数确保在UI更改之前已关闭数据库:
Future<void> _close(Database db) async {
await db.close();
}该函数可以放在db变量作用域中的任何位置,当您完成从数据库读取时,应该使用代码await _close();调用它。
https://stackoverflow.com/questions/61113128
复制相似问题