Home > Enterprise >  cubit returns a null value
cubit returns a null value

Time:10-19

I am facing very weird problem. i am using bloc with freezed, injectable and dartz. i just need to get data from SQl database and display it when a today page opened.

The code of UI is:

class TodayPage extends HookWidget {
  const TodayPage();
  @override
  Widget build(BuildContext context) {
    return BlocProvider<ScheduledNotesCubit>(
      lazy:false,
      create: (context) => getIt<ScheduledNotesCubit>()
        ..countDoneNoteOutOfAllNotes()
        ..retrieveData(),
      child: BlocBuilder<ScheduledNotesCubit, ScheduledNotesState>(
        builder: (context, state) {
          return ListView.builder(
            itemCount: state.maybeMap(
                orElse: () {}, getNotesCount: (g) => g.noteCount),
            itemBuilder: (BuildContext context, int index) {
            return  Text(
                "${state.maybeMap(orElse: () {}, getNotes: (notes) {
                      return notes.getNotes[index]['content'];
                    })}",
              );
            },
          );
        },
      ),
    );
  }
}

The code of state is:

@freezed
class ScheduledNotesState with _$ScheduledNotesState {
  const factory ScheduledNotesState.initial() = _Initial;
  const factory ScheduledNotesState.getNotes({required List<Map<String, dynamic>> getNotes}) = _GetNotes;
  const factory ScheduledNotesState.getNotesCount({required int noteCount}) = _GetNotesCount;
  const factory ScheduledNotesState.getCountDoneNoteOutOfAllNotes({required String getCountDoneNoteOutOfAllNotes}) = _GetCountDoneNoteOutOfAllNotes;
  const factory ScheduledNotesState.updateIsDoneNote({required int updateIsDoneNote}) = _UpdateIsDoneNote;
}

The code of cubit is:

@injectable
class ScheduledNotesCubit extends Cubit<ScheduledNotesState> {
  ScheduledNotesCubit(this._noteRepository)
      : super(const ScheduledNotesState.initial());
  final NoteRepository _noteRepository;

  // retrieve data
  void retrieveData() async {
   return emit(ScheduledNotesState.getNotes(
        getNotes: await _noteRepository.retrieveData()));
  }
}

This Cubit Does not return a value in listView, instead it returns null values, But When i try to do so it works!!!!!!

the updated cubit code is:

@injectable
class ScheduledNotesCubit extends Cubit<ScheduledNotesState> {
  ScheduledNotesCubit(this._noteRepository)
      : super(const ScheduledNotesState.initial());
  final NoteRepository _noteRepository;

  // retrieve data
  void retrieveData() async {
    var d= await _noteRepository.retrieveData(); //-->updated
   var x= d[1]['content']; //-->updated
    print("\n $x \n") ; // -->updated
   return emit(ScheduledNotesState.getNotes(
        getNotes: await _noteRepository.retrieveData()));
  }
}

CodePudding user response:

Can you try add lazy to false for BlocProvider, and update this code:

  void retrieveData() {
    _noteRepository.retrieveData().then((value) {
      emit(ScheduledNotesState.getNotes(getNotes: value));
    });
  }

CodePudding user response:

The solution is create a data class for the cubit, instead of creating a sealed classes.

  • Related