3

我有这些密封类用于肘部的状态:

part of 'logged_out_nickname_cubit.dart';

@freezed
abstract class LoggedOutNickNameState with _$LoggedOutNickNameState {
  factory LoggedOutNickNameState.initialised(
          LoggedOutNickNameViewModel vm, String nickNameKey, FormGroup form) =
      _Initialised;
  factory LoggedOutNickNameState.submitted(LoggedOutNickNameViewModel vm) =
      _Submitted;
}

这里是肘:

import 'package:bloc/bloc.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:reactive_forms/reactive_forms.dart';
import 'package:vepo/domain/user_registration/use-cases/logged_out_use_cases/get_user_field_from_local_use_case.dart';
import 'package:vepo/presentation/modules/logged_out/logged_out_nickname/logged_out_nick_name_view_model.dart';

part 'logged_out_nickname_state.dart';
part 'logged_out_nickname_cubit.freezed.dart';

class LoggedOutNickNameCubit extends Cubit<LoggedOutNickNameState> {
  LoggedOutNickNameCubit(
      this.vm, this.nickNameKey, this.form, this.getUserFieldFromLocalUseCase)
      : super(LoggedOutNickNameState.initialised(vm, nickNameKey, form)) {
    form.control(nickNameKey).value =
        getUserFieldFromLocalUseCase.invoke(nickNameKey);

    form.markAsDirty();
  }

  void submitPressed() => emit(LoggedOutNickNameState.submitted(vm));
  void initialise() =>
      emit(LoggedOutNickNameState.initialised(vm, nickNameKey, form));

  final LoggedOutNickNameViewModel vm;
  final String nickNameKey;
  final FormGroup form;
  final GetUserFieldFromLocalUseCase getUserFieldFromLocalUseCase;
}

问题是,在视图中,我尝试访问loggedOutNicknameCubit.state.form并且由于密封类之一上不存在该表单,因此它不被识别为处于 bloc 状态,并且不能在我的视图中使用:

没有为“LoggedOutNickNameState”类型定义吸气剂“表单”

解决方法是将该字段添加到所有不将其用作可为空的密封类中,如下所示:

factory LoggedOutNickNameState.submitted(LoggedOutNickNameViewModel vm,
      {@nullable @Default(null) FormGroup form}) = _Submitted;

有没有办法消除不涉及将字段作为 null 添加到不需要它的密封类的错误?

4

1 回答 1

0

您可以使用 cast 来获取这些变量

(loggedOutNicknameCubit.state as _Initialised).form
于 2021-05-01T08:17:41.560 回答