如果我有一个ListView来显示sql的数据。 1)我应该在哪里但是此功能关闭数据库? 2)_Close功能足以关闭它,或者我需要使用异步?
void _Close(Database db) {
db.close();
}
这是我的列表视图:
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");
},
),
);
});
}
答案 0 :(得分:1)
使用异步功能来确保在UI更改之前已关闭数据库:
Future<void> _close(Database db) async {
await db.close();
}
此函数可以放在db
变量范围内的任何位置,从数据库中读取完数据后,应使用代码await _close();
进行调用。