列表未在删除项目时更新

时间:2019-12-16 07:08:28

标签: listview flutter

我正在从列表视图中删除一个项目。它已成功删除,但未在UI上更新。当我返回上一屏幕并再次回到删除该项目的屏幕时,它会更新。我正在使用Dismissible删除项目。

下面是我设置列表视图的代码

 Expanded(
        child: ListView.builder(
          itemCount: data.length,
          itemBuilder: (ctx, i) => DataItem(
            data: data[i],
            shopList: widget.shopList,
            ),
        ),
      )

这是我在DataItem中的代码:

return Dismissible(
  key: UniqueKey(),
  background: Container(
    color: Theme.of(context).errorColor,
    child: Icon(
      Icons.delete,
      color: Colors.white,
      size: 40,
    ),
    alignment: Alignment.centerRight,
    padding: EdgeInsets.only(right: 20),
    margin: EdgeInsets.symmetric(
      horizontal: 15,
      vertical: 4,
    ),
  ),
  direction: DismissDirection.endToStart,
  confirmDismiss: (direction) {
    return showDialog(
      context: context,
      builder: (ctx) => AlertDialog(
        title: Text('Are you sure?'),
        content: Text('Do you want to remove from the cart?'),
        actions: <Widget>[
          FlatButton(
            child: Text('No'),
            onPressed: () {
              Navigator.of(ctx).pop(false);
            },
          ),
          FlatButton(
            child: Text('Yes'),
            onPressed: () {
              Navigator.of(ctx).pop(true);
            },
          )
        ],
      ),
    );
  },
  onDismissed: (direction) {

    setState(() {
      deleteCartItem(widget.data.id);
      String gen =  widget.data.toString();

    });
  },
  child: Card(
    margin: EdgeInsets.symmetric(
      horizontal: 15,
      vertical: 4,
    ),
    child: Padding(
      padding: EdgeInsets.all(8),
      child: ListTile(
        leading: CircleAvatar(
          backgroundColor: AppColors.purple,
          child: Padding(
            padding: EdgeInsets.all(5),
            child: FittedBox(
              child: Text(
                widget.generation.price,
                style: TextStyle(color: Colors.white),
              ),
            ),
          ),
        ),
        title: Text(widget.generation.title),
        subtitle: Text(
          'Total: \$${(widget.data.price * 1)}',
        ),

      ),
    ),
  ),
);

1 个答案:

答案 0 :(得分:0)

我已使用提供程序解决了此问题。我以前使用sqflite来获取全局数据。现在,将所有内容更改为提供程序后,我就解决了这个问题