如何在bloc [flutter]上列出项目复制
我想改变List
下面是我的示例块代码。
请教我如何正确使用copyWith。
[示例代码]
class StateA extends Equatable {
List<Map<String, dynamic>> listMap;
StateA({ required this.listMap});
StateA copyWith(
{List<Map<String, dynamic>>? listMap,}) {
return StateA(
listMap: listMap ?? this.listMap);
}
}
class CubitA extends Cubit<StateA> {
CubitA() : super(StateA(listMap: []));
void testCopy() {
emit(state.copyWith(listMap: [{"test_key": "test_value"}]));
print(state.listMap); // => []
}
(我英语不好,抱歉)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我解决它添加对象?
代码如下。
I solve it to add object?.
The code is below.
因此,应使用此上下文中的 copy with 函数将地图添加到地图列表中。
此函数将允许您将项目添加到当前状态的 listMap 并发出新状态。
建议:
不要将 copyWith 函数的参数作为可选参数(去掉问号)。
您必须重写 props 函数才能使 equatable 正常工作。请参阅文档或一些视频来了解这一点。
如果您对 flutter 很陌生,请忽略这一点..但我建议您浏览冻结的课程视频以创建块状态。我将在下面链接一个视频。它生成 copyWith 和 equals 操作的所有样板代码。
https://youtu.be/ApvMmTrBaFI
So the copy with function in this context should be used to add maps to the list of maps.
This function will allow you to add items to the current state's listMap and emit a new state.
Suggestions:
Don't make the the copyWith function's parameter as an optional parameter (remove the question mark).
You'll have to override the props function for equatable to work. Refer to the docs or some video to get an understanding of that.
If you're very new to flutter ignore this.. but I'd recommend going through the freezed class videos to make bloc state. I'll link a video below. It generates all the boilerplate code for copyWith and equality operations.
https://youtu.be/ApvMmTrBaFI
这个工作
Bloc 状态:
像这样发出:
this working
Bloc state:
Emit like this:
您必须为您的 StateA 创建一个对象,然后通过该对象访问副本。
you have to create a object for your StateA and then access the copywith through that object.