【发布时间】:2020-06-28 14:51:06
【问题描述】:
好吧,也许我用错了。我想在我的应用中保存一个笔记,然后更新主屏幕上的列表视图,以便它显示新的笔记...听起来很简单。
但是...保存笔记事件最后发生,主屏幕上的重新获取笔记首先发生,尽管它们在代码中是相反的:
编辑笔记屏幕:
void handleSaveNewNote() async {
BlocProvider.of<NoteDetailBloc>(context).add(SaveNewNoteEvent(note: currentNote)); //this is happening last
resetAfterSave();
}
void resetAfterSave() async {
BlocProvider.of<NoteListBloc>(context)..add(RefreshNoteListEvent()); /// this is happening first
Navigator.pop(context);
}
注意细节块:
if (event is SaveNewNoteEvent) {
note = await notesRepository.addNote(event.note);
print('new note is now saved'); // this shows as the very last print statement, but should be the first
yield NoteDetailLoadedState(note: note);
}
笔记列表块:
if (event is RefreshNoteListEvent) {
yield initialState;
List<NotesModel> notesList = await notesRepository.fetchNotes();
print('Bloc: i have awaited fetching $notesList'); // this shows as the very first print statement, but should be last... argh!
yield NoteListLoadedState(notesList: notesList);
}
我正在拔头发。为什么最后一个事件先发生,第一个事件最后发生?
【问题讨论】: