我正在学习冻结的颤振,我正在尝试从 sqflite 检索数据。我有一个没有集团模式的成就,它工作得很好。但是当我使用 bloc 这样做时,返回值始终为 null!。
单击此按钮时,它应该检索数据:
BlocBuilder<DisplayNoteCubit, DisplayNoteState>(
builder: (context, state) {
return MaterialButton(
onPressed: () async {
print( '\n print all Data \n ${state.maybeMap(orElse: () {}, getData: (g) => g.notes)}');
},
child: const Text("Click Me"),
);
},
),
块代码是:
final DBHelper _db=DBHelper.instance;
class DisplayNoteCubit extends Cubit<DisplayNoteState> {
DisplayNoteCubit() : super(const DisplayNoteState.initial());
void getData() {
state.maybeMap(
orElse: () {},
getData: (getData) async{
emit(getData.copyWith(notes:await _db.getAllNotesFormDB()));
});
}
}
状态代码是:
@freezed
abstract class DisplayNoteState with _$DisplayNoteState {
const factory DisplayNoteState.initial() = Initial;
const factory DisplayNoteState.getData({required List<Map<String, dynamic>> notes}) = GetData;
}