【问题标题】:how to fix flutter blocConsumer state receiver null error如何修复颤振 blocConsumer 状态接收器空错误
【发布时间】:2021-01-20 19:13:57
【问题描述】:

大家好,我正在关注关于 DDD 的 reso coder 系列,我在第八部分遇到错误,这是我的代码:

return BlocConsumer<SignInFormBloc, SignInFormState>(
    builder: (context, state) {},
    listener: (context, state) {});

这是错误:

The following NoSuchMethodError was thrown building BlocConsumer<SignInFormBloc, SignInFormState>:
The getter 'state' was called on null.
Receiver: null
Tried calling: state

这是 sign_in_form_state 代码:

part of 'sign_in_form_bloc.dart';
@freezed
abstract class SignInFormState with _$SignInFormState {
  const factory SignInFormState({
    @required EmailAddress emailAddress,
    @required Password password,
    @required bool showErrorMessages,
    @required bool isSubmitting,
    @required Option<Either<AuthFailure, Unit>> authFailureOrSuccessOption,
  }) = _SignInFormState;

  factory SignInFormState.initial() => SignInFormState(
        emailAddress: EmailAddress(' '),
        password: Password(' '),
        showErrorMessages: false,
        isSubmitting: false,
        authFailureOrSuccessOption: none(),
      );
}

提前致谢。

【问题讨论】:

  • 贴出相关代码,你在哪里使用state?

标签: flutter domain-driven-design bloc


【解决方案1】:

这个错误是因为我更新了软件包版本而没有更新我的代码,因为使用 update comme new 方法和声明错误来自 sign_in_form_bloc 而不是 sign_in_form_state 所以我修改了这一行 旧行:

SignInFormBloc(SignInFormState initialState, this._authFacade)
      : super(initialState);

换行:

SignInFormBloc(this._authFacade) : super(SignInFormState.initial());

injection.config.dart 从此改变:

 gh.factory<SignInFormBloc>(() => SignInFormBloc(get<SignInFormState>,get<IAuthFacade>()));

到这里:

 gh.factory<SignInFormBloc>(() => SignInFormBloc(get<IAuthFacade>()));

【讨论】:

    猜你喜欢
    • 2020-01-27
    • 2021-04-08
    • 2020-02-12
    • 2021-11-20
    • 2019-09-28
    • 2019-12-02
    • 1970-01-01
    • 1970-01-01
    • 2019-06-03
    相关资源
    最近更新 更多