您如何在DART(Bloc/Flutter)中以有效的方式进行此列表操作?

发布于 2025-02-11 07:56:49 字数 1543 浏览 1 评论 0原文

我有以下状态包含2个单词的bloc,

// 2 words are fixed and same length.
// in word_state.dart
abstract class WordState extends Equatable 
  const WordState(this.quest, this.answer, this.word, this.clicked);
  final List<String> wordA; // WordA = ['B','A','L',L']
  final List<String> wordB; // WordB = ['','','','']
  @override
  List<Object> get props => [wordA,wordB];
}

我想添加和删除字母。

// in word_event.dart
class AddLetter extends WordEvent {
  final int index;
  const AddLetter(this.index);
}
class RemoveLetter extends WordEvent {
  final int index;
  const RemoveLetter(this.index);
}

1.Add: 如果我在Worda中选择“ L”的索引,则在WordB中首次出现“(空)的第一次出现”中添加字母“ L”。

// in word_bloc.dart 
void _onLetterAdded(AddLetter event, Emitter<WordState> emit) {
  final b = [...state.wordB]; 
  b[b.indexOf('')] = state.wordA[event.index];
  emit(WordLoaded(state.wordA, b));
}
//wordB, ['','','',''] into ['L','','','']

2.示威: 如果我取消了Worda中“ L”索引的选择,那么我删除WordB中字母“ L”的最后一个事件,然后将右侧字母转移到左侧

 void _onLetterRemoved(RemoveLetter event, Emitter<WordState> emit) {
    final b = [...state.wordB];
    final index = b.lastIndexOf(state.wordA[event.index]);
    for (int i = index; i < 4 - 1; i++) { 
      b[i] = b[i + 1];
    }
    b[3] = '';
    emit(WordLoaded(state.wordA, b));
  }
}
// What i am trying to
// ['B','L','A','L']
// if index is 1 then ['B','A','L','']

,但我想有效地进行列表操作。

I have BLoC with the following state which contains 2 words ,

// 2 words are fixed and same length.
// in word_state.dart
abstract class WordState extends Equatable 
  const WordState(this.quest, this.answer, this.word, this.clicked);
  final List<String> wordA; // WordA = ['B','A','L',L']
  final List<String> wordB; // WordB = ['','','','']
  @override
  List<Object> get props => [wordA,wordB];
}

I want to ADD and REMOVE letters.

// in word_event.dart
class AddLetter extends WordEvent {
  final int index;
  const AddLetter(this.index);
}
class RemoveLetter extends WordEvent {
  final int index;
  const RemoveLetter(this.index);
}

1.ADD:
If I select the index of 'L' in wordA, then I add the letter 'L' in the first occurrence of '' (empty) in wordB.

// in word_bloc.dart 
void _onLetterAdded(AddLetter event, Emitter<WordState> emit) {
  final b = [...state.wordB]; 
  b[b.indexOf('')] = state.wordA[event.index];
  emit(WordLoaded(state.wordA, b));
}
//wordB, ['','','',''] into ['L','','','']

2.REMOVE:
If I deselect the index of 'L' in wordA, then I remove the last occurence of letter 'L' in wordB and shift the right side letters to left

 void _onLetterRemoved(RemoveLetter event, Emitter<WordState> emit) {
    final b = [...state.wordB];
    final index = b.lastIndexOf(state.wordA[event.index]);
    for (int i = index; i < 4 - 1; i++) { 
      b[i] = b[i + 1];
    }
    b[3] = '';
    emit(WordLoaded(state.wordA, b));
  }
}
// What i am trying to
// ['B','L','A','L']
// if index is 1 then ['B','A','L','']

This code is working fine, But I want to do the list operations in efficient way.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

优雅的叶子 2025-02-18 07:56:49

您可以检查此代码理解并在Dart Pad上运行它。

  List<String> wordA=['B','A','L','L'];
  List<String> wordB=['','','',''];

  List<String> wordBAll=['B','L','A','L'];
  void main() {
  wordB.insert(0,wordA[2]);
  wordBAll.removeAt(wordBAll.length-1);
  print(wordB);
  print(wordBAll);

}

can you please check this code understand it and run it on dart pad.

  List<String> wordA=['B','A','L','L'];
  List<String> wordB=['','','',''];

  List<String> wordBAll=['B','L','A','L'];
  void main() {
  wordB.insert(0,wordA[2]);
  wordBAll.removeAt(wordBAll.length-1);
  print(wordB);
  print(wordBAll);

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文