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

Flet Python购物车展示报错:'list'对象无'_set_attr_internal'属性

问题分析与解决方案

错误根源

这个AttributeError的核心原因是Flet的ft.Container组件的content属性被传入了一个列表。Flet要求Container的content必须是单个Flet控件(如Row/Column),不能直接传入多个控件组成的列表——列表不是Flet的控件实例,自然没有_set_attr_internal方法,从而触发错误。

另外代码还有一处小问题:函数参数定义的是cart_list,但循环里用了for i in cart,这会导致变量未定义错误,需要统一变量名。

修正后的代码

order.py 中 prods_list 函数

def prods_list(pg, cart_list):
    added_buttons = []

    for i in cart_list:
        # 把Container内的多个控件用Row包裹,作为Container的content
        button = ft.Row([
            ft.Container(
                content=ft.Row([
                    ft.Image(src=i[3], width=200, height=250, expand=True),
                    ft.Column([
                        ft.Text(value=i[2], size=20, weight=ft.FontWeight.W_500),
                        ft.Text(value=f"{i[-2]} ₽", size=20, weight=ft.FontWeight.W_500),
                    ], alignment=ft.MainAxisAlignment.SPACE_AROUND),
                    ft.IconButton(icon="remove", disabled=condition_quantity(i[-1], icon="remove"), data="mashed_potatoes", on_click=quantity_change),
                    ft.Text(value=i[-1], data="mashed_potatoes", size=20, color="black", weight=ft.FontWeight.W_200),
                    ft.IconButton(icon="add", disabled=condition_quantity(i[-1], icon="add"), data="mashed_potatoes", on_click=quantity_change),
                    ft.IconButton(icon="delete", disabled=condition_quantity(i[-1], icon="delete"), data="mashed_potatoes", on_click=quantity_change),
                ])
            ),
        ])

        added_buttons.append(button)

    return added_buttons

主文件调用部分

建议明确指定controls参数,代码可读性更强:

c = ft.Column(controls=order.prods_list(pg, cart))
pg.add(c)

额外优化提示

  • 所有IconButton和Text的data都硬编码为"mashed_potatoes",建议改为商品唯一标识(如i[0],假设是商品ID),这样quantity_change函数才能区分操作的是哪个商品。
  • 检查condition_quantity函数的逻辑,确保它能正确返回布尔值控制按钮禁用状态。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.19 12:10:01