Flutter BLOC 状态更新但始终为空值

Flutter BLOC state updating but always null value

我在 bloc 文件中像这样更新状态 bloc 生成器上下文 context.read<SurveyBloc>().add(SurveyModeChanged(mode: 'draft')); 触发了状态更改,但值始终为 null。最近 2 天我遇到了这个问题,请帮助解决这个问题。

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

这是调查屏幕文件

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

这是我的调查活动代码

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 {}

调查状态飞镖

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