Closed rizirf-connect closed 3 years ago
Try the code below for the getCurrentPosition event. If this works then use this approach approach with the other event.
NB: elementAt is inefficient for lists because it will traverse the list up to the index,it's supposed to be used for lazy iterables
getCurrentPosition: (e) async* {
if (state.lastPlayingPara != Para.empty()) {
//no need to create a copy just to get the index, also types are inferred
final index = state.audioItems.indexWhere((e) => e.para == state.lastPlayingPara);
//no copy needed here either.
final newAudioItem = state.audioItems[index].copyWith(audioPosition: e.position);
final itemsCopy = List<AudioItem>.from(state.audioItems);
itemsCopy
..removeAt(index);
..insert(index, newAudioItem);
yield state.copyWith(audioItems: itemsCopy);
}
},
Thanks @jinyus this is working fine but when i remove note from para the bloc builder does not rebuild state
removeNoteAt: (e) async* {
final newPara = state.playlist.audioItems[lastPlayingItemIndex].para
..notes.removeAt(e.index);
final newAudioItem = state.playlist.audioItems[lastPlayingItemIndex]
.copyWith(para: newPara);
final itemsCopy = List<AudioItem>.from(state.playlist.audioItems);
itemsCopy
..removeAt(lastPlayingItemIndex)
..insert(lastPlayingItemIndex, newAudioItem);
yield state.copyWith(playlist: AudioItems(audioItems: itemsCopy));
}
That's because you're modifying the old para. newPara in your snippet still references the old para. So create a new instance of the para.
final oldPara = state.playlist.audioItems[lastPlayingItemIndex].para;
final newParaNotes = oldPara.notes..removeAt(e.index);
final newPara = oldPara.copyWith(notes:newParaNotes);
That should work since newPara now references a new object. If this doesn't work trying making a copy of newParaNotes;
Thanks you @jinyus You are life saver man.
Hey @felangel can you please tell me why there is no changes occur in the list
AudioPlayerBloc.dart
AudioPlayerState.dart