在Bloc中颤振NoSuchMethodError

时间:2020-06-24 00:58:31

标签: flutter

我被卡住了一点,在我的flutter应用程序中有一个Bloc,当相机找到qr码时,需要将其称为配置检查(isConfigured())。但是我总是遇到 [ERROR:flutter / lib / ui / ui_dart_state.cc(157)]未处理的异常:未处理的错误NoSuchMethodError:方法“ isConfigured”在null上调用。 接收者:null 尝试调用:isConfigured()发生在“ QrBloc”的bloc实例中。

这是集团

class QrBloc extends Bloc<QrEvent, QrState> {
  final ValidateTicket validateTicket;
  final ConfigureCode configureCode;
  final QrCodeRepository repository;

  QrBloc(
      {@required ValidateTicket validateTicket,
      @required ConfigureCode configureCode,
      this.repository})
      : assert(validateTicket != null, configureCode != null),
        validateTicket = validateTicket,
        configureCode = configureCode;

  @override
  QrState get initialState => InitialState();

  @override
  Stream<QrState> mapEventToState(
    QrEvent event,
  ) async* {
    if (event is QrCodeScanned) {
      final bool isConfigured = await repository.isConfigured();
      if (isConfigured) {
        final result = await validateTicket(ValidateParams(code: event.qrCode));
        yield* _eitherTicketOrErrorState(result);
      } else {
        yield ConfigurationState();
      }
    } else if (event is SaveConfiguration) {
      final failureOrNot =
          await configureCode(ConfigureParams(code: event.qrCode));
      yield* _eitherConfigOrErrorState(failureOrNot);
    }
  }

  Stream<QrState> _eitherConfigOrErrorState(
    Either<Failure, ConfigurationQrCodeModel> either,
  ) async* {
    yield either.fold(
      (failure) => Error(message: _mapFailureToMessage(failure)),
      (qrCode) => InitialState(),
    );
  }

  Stream<QrState> _eitherTicketOrErrorState(
    Either<Failure, TicketModel> either,
  ) async* {
    yield either.fold(
      (failure) => Error(message: _mapFailureToMessage(failure)),
      (qrCode) {
        if (qrCode.ticketValid == true) {
          return ValidTicketState(ticket: qrCode);
        } else {
          return InvalidTicketState(ticket: qrCode);
        }
      },
    );
  }

  String _mapFailureToMessage(Failure failure) {
    switch (failure.runtimeType) {
      case ServerFailure:
        return serverExceptionText;
      default:
        return unexpectedExceptionText;
    }
  }
}

这是电话

sl()。add(QrCodeScanned(qrCode:data));

1 个答案:

答案 0 :(得分:0)

QrBloc最初在哪里实例化?我相信存储库字段为空。通常,当它说“方法'xyz'被调用为null”时。这意味着从空值调用了该特定方法。