【发布时间】:2021-07-14 13:00:03
【问题描述】:
我创建了一个字符串列表List<String> favId;,以便在用户单击收藏图标时获取列表项的 ID。但是当我这样写代码时:
GestureDetector(
onTap: () {},
child: IconButton(
icon: Icon(
doa.fav ? Icons.favorite : Icons.favorite_border,
color: doa.fav ? Colors.red : Colors.grey,
),
onPressed: () => setState(() {
doa.fav = !doa.fav;
favId.add(doa.id.toString());
}),
),
)
编译器出错并显示此消息:
The method 'add' was called on null.
Receiver: null
Tried calling: add("2")
如何修复我的代码,以便将用户单击的项目 ID 添加到 favId 中。 这是点击 IconButton 时的预览:
【问题讨论】:
-
你的
favId在哪里初始化?