更新对象列表而不复制它 Flutter Bloc

huangapple go评论63阅读模式
英文:

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 in the bloc file I have to make a copy for every single element in the list to successfully emit a new list even if I need to change or add only 1 element!

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&#39;m new with using flutter bloc state management and Equatable package , when I need to update List&lt;Objects&gt; 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&lt;Object?&gt; get props =&gt; [
                    id,
                    content,
                  ];
            }
        
        //bloc file
             Futurer&lt;void&gt; _onUpdateMessagesMessageEvent(
                  UpdateMessagesMessageEvent event, Emitter&lt;Bloctate&gt; emit) async {
                String messageId = Uuid().v4();
               List&lt;Message&gt; messages = List.generate(state.messages.length, (index) =&gt;state.messages[index].copyWith());
                messages.add(Message(
                        id: messageId,
                        content: event.textEditingController.text,
                       ));
            
                emit(BlocState(messages: messages));
            
              }
        
        //state file
            class BlocState extends Equatable {
              List&lt;Message&gt; messages;
              BlocState({
                required this.messages,
                });
              @override
              List&lt;Object&gt; get props =&gt; [
                    messages,];
            }

 I need to update &#39;messages&#39; 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&lt;MessageState&gt; 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&lt;MessageState&gt; emit) {
final id = const Uuid().v4();
emit(MessageState(
messages: List.of(state.messages)..add(Message(id, event.content))));
}

huangapple
  • 本文由 发表于 2023年4月11日 07:23:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75981418.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定