# DocumentFormBloc constructor Null safety

DocumentFormBloc(

1. String? appId,
2. {FormAction? formAction}

)

## Implementation

    DocumentFormBloc(this.appId, {this.formAction})
        : super(DocumentFormUninitialized()) {
      on<InitialiseNewDocumentFormEvent>((event, emit) {
        DocumentFormLoaded loaded = DocumentFormLoaded(
            value: DocumentModel(
          documentID: "",
          appId: "",
          description: "",
          content: "",
          padding: 0.0,
          images: [],
        ));
        emit(loaded);
      });

      on<InitialiseDocumentFormEvent>((event, emit) async {
        // Need to re-retrieve the document from the repository so that I get all associated types
        DocumentFormLoaded loaded = DocumentFormLoaded(
            value: await documentRepository(appId: appId)!
                .get(event.value!.documentID));
        emit(loaded);
      });
      on<InitialiseDocumentFormNoLoadEvent>((event, emit) async {
        DocumentFormLoaded loaded = DocumentFormLoaded(value: event.value);
        emit(loaded);
      });
      DocumentModel? newValue;
      on<ChangedDocumentDocumentID>((event, emit) async {
        if (state is DocumentFormInitialized) {
          final currentState = state as DocumentFormInitialized;
          newValue = currentState.value!.copyWith(documentID: event.value);
          if (formAction == FormAction.addAction) {
            emit(await _isDocumentIDValid(event.value, newValue!));
          } else {
            emit(SubmittableDocumentForm(value: newValue));
          }
        }
      });
      on<ChangedDocumentDescription>((event, emit) async {
        if (state is DocumentFormInitialized) {
          final currentState = state as DocumentFormInitialized;
          newValue = currentState.value!.copyWith(description: event.value);
          emit(SubmittableDocumentForm(value: newValue));
        }
      });
      on<ChangedDocumentContent>((event, emit) async {
        if (state is DocumentFormInitialized) {
          final currentState = state as DocumentFormInitialized;
          newValue = currentState.value!.copyWith(content: event.value);
          emit(SubmittableDocumentForm(value: newValue));
        }
      });
      on<ChangedDocumentPadding>((event, emit) async {
        if (state is DocumentFormInitialized) {
          final currentState = state as DocumentFormInitialized;
          if (isDouble(event.value!)) {
            newValue =
                currentState.value!.copyWith(padding: double.parse(event.value!));
            emit(SubmittableDocumentForm(value: newValue));
          } else {
            newValue = currentState.value!.copyWith(padding: 0.0);
            emit(PaddingDocumentFormError(
                message: "Value should be a number or decimal number",
                value: newValue));
          }
        }
      });
      on<ChangedDocumentImages>((event, emit) async {
        if (state is DocumentFormInitialized) {
          final currentState = state as DocumentFormInitialized;
          newValue = currentState.value!.copyWith(images: event.value);
          emit(SubmittableDocumentForm(value: newValue));
        }
      });
      on<ChangedDocumentBackground>((event, emit) async {
        if (state is DocumentFormInitialized) {
          final currentState = state as DocumentFormInitialized;
          newValue = currentState.value!.copyWith(background: event.value);
          emit(SubmittableDocumentForm(value: newValue));
        }
      });
      on<ChangedDocumentConditions>((event, emit) async {
        if (state is DocumentFormInitialized) {
          final currentState = state as DocumentFormInitialized;
          newValue = currentState.value!.copyWith(conditions: event.value);
          emit(SubmittableDocumentForm(value: newValue));
        }
      });
    }