You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter中基于Sqflite动态生成删除按钮并实现指定数据删除的问题

Flutter Sqflite删除按钮功能实现

我想要实现删除按钮功能,让用户可以删除自己选中查看的对应数据。

如下图所示,Sqflite中每新增一条数据,页面上就会对应新增一个删除按钮:

  • 仅有1条数据时的页面效果:单条数据效果图
  • 存在多条数据时的页面效果:多条数据效果图

以下是我当前实现删除功能及页面构建的代码:

FutureBuilder(
    future: DatabaseHelper.instance.getCards(),
    builder: (BuildContext context, AsyncSnapshot<List<Cards>> snapshot) {
      return SizedBox(
        width: 40.w,
        child: Column(
          children: snapshot.data!.map((cards) {
            return SizedBox(
              width: 40.w,
              child: RaisedButton(
                color: Color(0xffBE4E49),
                textColor: Colors.white,
                shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(10)),
                onPressed: () async {
                  // null;
                  setState(() {
                    DatabaseHelper.instance.remove(cards.id!);
                  });
                  Navigator.pushAndRemoveUntil(
                    context,
                    MaterialPageRoute(
                        builder: (_) => new HomeScreen()),
                        (Route<dynamic> route) => false,
                  );
                },
                child: Text(
                  'DELETE',
                  style: TextStyle(
                      fontSize: 13.sp,
                      fontWeight: FontWeight.w700,
                      fontFamily: 'Poppins'),
                ),
              ),
            );
          }).toList(),
        ),
      );
    }),

内容的提问来源于stack exchange,提问作者Caramel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.07 15:45:02