2

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);
    }
  }
}

1 Answer 1

3
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);
    }
  }
}
2
  • state.mode to event.bode thanks simple but serious mistake, saved my time thank you.
    – yathavan
    May 1, 2021 at 14:29
  • I'm glad to have helped :)
    – user14280337
    May 1, 2021 at 14:32

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.