Flutter BLOC state updating but always null value

Issue

I update state bloc builder context like this context.read<SurveyBloc>().add(SurveyModeChanged(mode: 'draft')); in the bloc file state changing is triggered but value always null. the last 2days I struck with this someone please help to resolve this issue.

if (event is SurveyModeChanged) {
      print('mode==>');
      print(state.mode);
      yield state.copyWith(mode: state.mode);
    }

This is Survey screen file

class SurveyView extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _SurveyViewState();
}

class _SurveyViewState extends State<SurveyView> {
  @override
  Widget build(BuildContext context) {
    final sessionCubit = context.read<SessionCubit>();

    return BlocProvider(
      create: (context) => SurveyBloc(
        user: sessionCubit.selectedUser ?? sessionCubit.currentUser,
        surveyId: '4aa842ff-2b7d-4364-9669-29c200a3fe9b',
        dataRepository: context.read<DataRepository>(),
      ),
      child: BlocListener<SurveyBloc, SurveyState>(
        listener: (context, state) {},
        child: Scaffold(
          backgroundColor: Color(0xFFF2F2F7),
          appBar: _appbar(),
          body: stepFormContainer(context),
          resizeToAvoidBottomInset: false,
        ),
      ),
    );
  }

  Widget saveButton() {
    return BlocBuilder<SurveyBloc, SurveyState>(builder: (context, state) {
      return Padding(
          padding: EdgeInsets.symmetric(horizontal: 10),
          child: ElevatedButton.icon(
              onPressed: () {
                context
                    .read<SurveyBloc>()
                    .add(SurveyModeChanged(mode: 'draft'));
              },
              label: Text('Save')));
    });
  }
}

This is my Survey event code

abstract class SurveyEvent {}
class SurveyResultChanged extends SurveyEvent {
  final String surveyResult;
  SurveyResultChanged({this.surveyResult});
}
class SurveyModeChanged extends SurveyEvent {
  final String mode;
  SurveyModeChanged({this.mode});
}
class SurveyIdChanged extends SurveyEvent {
  final String surveyId;
  SurveyIdChanged({this.surveyId});
}

class SaveSurveyChanges extends SurveyEvent {}

Survey State dart

class SurveyState {
  final User user;
  final FormSubmissionStatus formSubmissionStatus;
  final String surveyId;
  final String mode;
  final String surveyResult;

  SurveyState(
      {@required User user,
      @required String surveyId,
      String mode,
      String surveyResult,
      this.formSubmissionStatus = const InitialFormStatus()})
      : this.user = user,
        this.surveyId = surveyId,
        this.mode = mode,
        this.surveyResult = surveyResult;

  SurveyState copyWith({
    User user,
    FormSubmissionStatus formSubmissionStatus,
    String surveyId,
    String mode,
    String surveyResult,
  }) {
    return SurveyState(
        user: user ?? this.user,
        surveyId: surveyId ?? this.surveyId,
        mode: mode ?? this.mode,
        surveyResult: surveyResult ?? this.surveyResult,
        formSubmissionStatus:
            formSubmissionStatus ?? this.formSubmissionStatus);
  }
}

SurveyBloc.dart

class SurveyBloc extends Bloc<SurveyEvent, SurveyState> {
  final DataRepository dataRepository;

  SurveyBloc({
    @required User user,
    @required String surveyId,
    this.dataRepository,
  }) : super(SurveyState(user: user, surveyId: surveyId));

  @override
  Stream<SurveyState> mapEventToState(SurveyEvent event) async* {
    if (event is SurveyModeChanged) {
      print('mode==>');
      print(state.mode);
      yield state.copyWith(mode: state.mode);
    }
  }
}

Solution

class SurveyBloc extends Bloc<SurveyEvent, SurveyState> {
  final DataRepository dataRepository;

  SurveyBloc({
    @required User user,
    @required String surveyId,
    this.dataRepository,
  }) : super(SurveyState(user: user, surveyId: surveyId));

  @override
  Stream<SurveyState> mapEventToState(SurveyEvent event) async* {
    if (event is SurveyModeChanged) {
      print('mode==>');
      print(state.mode);
      // This is where the problem occurs. You are emitting the state
      // value again and again which is null. Change this:
      yield state.copyWith(mode: state.mode);
      // into this:
      yield state.copyWith(mode: event.mode);
    }
  }
}

Answered By – user14280337

Answer Checked By – Marie Seifert (FlutterFixes Admin)

Leave a Reply

Your email address will not be published.