簡體   English   中英

Flutter BLOC state 更新但總是 null 值

[英]Flutter BLOC state updating but always null value

我更新 state bloc builder 上下文,如下所示context.read<SurveyBloc>().add(SurveyModeChanged(mode: 'draft')); 在 bloc 文件 state 中觸發更改,但值始終為 null。 在過去的兩天里,我遇到了這個問題,請幫助解決這個問題。

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM