英文:
update list of objects without copy it Flutter Bloc
问题
I'm new with using flutter bloc state management and Equatable package, when I need to update List
Like the example below...
class Message extends Equatable {
Message({
required this.id,
required this.content,
});
final String id;
final String content;
Message copyWith({
String? id,
String? content,
}) {
return Message(
id: id ?? this.id,
content: content ?? this.content,
);
}
@override
List<Object?> get props => [
id,
content,
];
}
// bloc file
Future<void> _onUpdateMessagesMessageEvent(
UpdateMessagesMessageEvent event, Emitter<BlocState> emit) async {
String messageId = Uuid().v4();
List<Message> messages =
List.generate(state.messages.length, (index) => state.messages[index].copyWith());
messages.add(Message(
id: messageId,
content: event.textEditingController.text,
));
emit(BlocState(messages: messages));
}
// state file
class BlocState extends Equatable {
List<Message> messages;
BlocState({
required this.messages,
});
@override
List<Object> get props => [
messages,
];
}
I need to update 'messages' list and add 1 element to it without being forced to copy all elements in the list.
<details>
<summary>英文:</summary>
I'm new with using flutter bloc state management and Equatable package , when I need to update List<Objects> in the bloc file I have to make a copy for every single element in the list to successfully emit new list even if I need to change or add only 1 element!
Like the example bellow..
class Message extends Equatable {
Message({
required this.id,
required this.content,
});
final String id;
final String content;
Message copyWith({
String? id,
String? content,
}) {
return Message(
id: id ?? this.id,
content: content ?? this.content,
);
}
@override
// TODO: implement props
List<Object?> get props => [
id,
content,
];
}
//bloc file
Futurer<void> _onUpdateMessagesMessageEvent(
UpdateMessagesMessageEvent event, Emitter<Bloctate> emit) async {
String messageId = Uuid().v4();
List<Message> messages = List.generate(state.messages.length, (index) =>state.messages[index].copyWith());
messages.add(Message(
id: messageId,
content: event.textEditingController.text,
));
emit(BlocState(messages: messages));
}
//state file
class BlocState extends Equatable {
List<Message> messages;
BlocState({
required this.messages,
});
@override
List<Object> get props => [
messages,];
}
I need to update 'messages' list and add 1 element to it without being forced to copy all elements in the list
</details>
# 答案1
**得分**: 2
Bloc状态应该是不可变的,这意味着你必须复制现有的`List`,将新的`Message`添加到复制的`List`中,然后发出一个新的状态。
```dart
void _onUpdate(UpdateMessage event, Emitter<MessageState> emit) {
final id = const Uuid().v4();
emit(MessageState(
messages: List.of(state.messages)..add(Message(id, event.content))));
}
英文:
Bloc States should be immutable meaning you have to copy the existing List
add new Message
to the copied List
and emit a new state.
void _onUpdate(UpdateMessage event, Emitter<MessageState> emit) {
final id = const Uuid().v4();
emit(MessageState(
messages: List.of(state.messages)..add(Message(id, event.content))));
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论