删除其中一项后,我无法更新我的债务清单。
我有一个带有底部导航栏和两个选项的主屏幕。检查列表或所有项目的常规信息。索引零是列表,因此默认情况下为绘制。
https://i.imgur.com/6qOVPPE.png
在我的main_screen.dart中,我有一个从SQLite数据库获得的ListbtList巫婆。我将此列表传递给每个构造函数。
Widget _getTab(int currentIndex) {
switch (currentIndex) {
case 0:
return DebtList(debtList);
case 1:
return GeneralInfo(debtList);
break;
default:
return DebtList(debtList);
}
}
现在我点击一个项目以访问该项目的信息。
https://i.imgur.com/ikl4cEI.png
当我单击删除时,它可以工作。该项目不再在数据库中,但列表未更新。
dbHelper.deleteDebt(widget.debt.id);
Navigator.pop(context, **true**);
我试图返回true,所以我可以这样检查它:
bool result = await Navigator.push(context, MaterialPageRoute(builder: (context) => DebtDetail(debt)));
if (result != null && result == true) {
...
但是updateList()方法在我的main_screen.dart类中,而不在我的btc_list.dart类中。
我在做什么错?我无法更新我的btl_list类中的列表,因为它是最终列表。
widget.debtList = debtList; //cant do this
我应该改变什么?
谢谢您的时间!
答案 0 :(得分:0)
您需要通知DebtList
知道要删除哪个列表项Debt
。由于您在DebtDetail
屏幕上有许多操作要更新update
,delete
等列表,因此最好定义一个DTO(数据传输对象)类以从{{1} }到DebtDetail
。
DebtList
现在,在class DebtActionResult {
int action; // 0: nothing, 1: update, 2: delete
int debtId;
Debt updateDebt;
DebtActionResult({this.action, this.debtId, this.updateDebt});
}
屏幕上,我们必须将结果传递到DebtDetail
DebtList
到目前为止,到目前为止,该在void _delete() {
DebtActionResult result = DebtActionResult(action: 2, debtId: widget.debt.id, null);
Navigator.pop(context, result);
}
void _update() {
DebtActionResult result = DebtActionResult(action: 1, debtId: widget.debt.id, newDebt); // handle this yourself, because you are adding `final` to the state. Remove the final or create a new `newDebt` state variable.
Navigator.pop(context, result);
}
屏幕上处理结果了。
DebtList