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

Posted

技术标签:

【中文标题】Flutter BLOC 状态更新但始终为空值【英文标题】:Flutter BLOC state updating but always null value 【发布时间】:2021-07-24 13:54:53 【问题描述】:

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

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

【问题讨论】:

【参考方案1】:
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);
    
  

【讨论】:

state.mode 到 event.bode 谢谢简单但严重的错误,节省了我的时间谢谢。 很高兴能帮上忙 :)

以上是关于Flutter BLOC 状态更新但始终为空值的主要内容,如果未能解决你的问题,请参考以下文章

为啥尽管观察者的状态发生变化,但应用程序重启后 Flutter BloC UI 没有更新?

Flutter Bloc - Flutter Bloc 状态未更新

Flutter bloc:更新状态不会重新调用 BlocBuilder

更新示例计数器中的非立即状态 - BloC Flutter

Flutter 状态管理之Bloc上

Flutter 状态管理之Bloc上