EditTraceabilityBloc constructor

EditTraceabilityBloc({
  1. EditTraceabilityState init = const EditTraceabilityState(),
})

Implementation

EditTraceabilityBloc({EditTraceabilityState init = const EditTraceabilityState()}) : super(init) {
  on<LoadImageTraceabilityEvent>((event, emit) => emit(LoadImageTraceabilityState()));
  on<DownloadFileEvent>((event, emit) async {
    final response = await ApiClient().getFile(event.url, timeout: 900);
    if (response != null) {
      try {
        Directory? folder;
        if (Platform.isIOS)
          folder = await getApplicationDocumentsDirectory();
        else folder = await DownloadsPathProvider.downloadsDirectory;
        if (folder == null) {
          emit(const DownloadFileState(false,''));
          return;
        }
        final array = event.url.split('/');
        final file = File(folder.path + '/' + array[array.length - 1]);
        print(folder.path + '/' + array[array.length - 1]);
        file.writeAsBytesSync(response);
        emit(DownloadFileState(true,folder.path));
      } catch (_) {
        emit(DownloadFileState(false,""));
      }
    } else emit(const DownloadFileState(false,""));
  });
  on<DownloadImagesTraceabilityEvent>((event, emit)async {
    final List<File> list = [];
    final String path = await _localPath();
    for(int i = 0; i < event.list.length; i++) {
      final File? file = await downloadImage(event.list[i].name, path);
      if (file != null) list.add(file);
    }
    emit(DownloadImagesTraceabilityState(list,event.index));
  });
  on<DownloadVideoTraceabilityEvent>((event, emit)async {
    final List<File> list = [];
    final String path = await _localVideoPath();
    final File? file = await downloadImage(event.list.name, path);
    if (file != null) list.add(file);
    emit(DownloadVideoTraceabilityState(list));
  });
  on<CreateTraceabilityEvent>((event, emit) async {
    final response = await ApiClient().postAPIOfTraceability('${Constants().apiVersion}traceabilities','POST',
        TraceabilityResutlModel(),hasHeader: true,body: {
          "name": event._traceability_product.name,
          "lot_code": event._traceability_product.lot_code,
          "packing_way": event._traceability_product.packing_way,
          "date_of_manufacture": event._traceability_product.date_of_manufacture,
          "number_of_quarantine_days": event._traceability_product.number_of_quarantine_days,
          "certification": event._traceability_product.certification,
          "description": event._traceability_product.description,
          "traceability_details_attributes[0][traceability_type]": "ingredient",
          "traceability_details_attributes[0][ingredient_name]": event._ingredient.ingredient_name,
          "traceability_details_attributes[0][land_area]": event._ingredient.land_area,
          "traceability_details_attributes[0][ingredient_quantily]": event._ingredient.ingredient_quantily,
          "traceability_details_attributes[0][ingredient_address]": event._ingredient.ingredient_address,
          "traceability_details_attributes[1][manufacture_name]": event._manufactor.manufacture_name,
          "traceability_details_attributes[1][traceability_type]": "manufactor",
          "traceability_details_attributes[1][manufacture_address]": event._manufactor.manufacture_address,
          "traceability_details_attributes[1][manufacture_phone]": event._manufactor.manufacture_phone,
          "traceability_details_attributes[1][manufacture_email]": event._manufactor.manufacture_email,
        },
      realFiles1: event.list_image_product,
      realFiles2: event.list_video_product,
        realFiles3: event.list_image_ingredient,
        realFiles4: event.logo,
      paramFile1: "attachment[file][]",
      paramFile2: "attachment_video[file][]",
      paramFile3: "traceability_details_attributes[0][attachment][file][]",
      paramFile4: "traceability_details_attributes[1][attachment][file][]"
    );
    emit(CreateTraceabilityState(response));
  });
  on<UpdateTraceabilityEvent>((event, emit) async {
    final response = await ApiClient().postAPIOfTraceability('${Constants().apiVersion}traceabilities/${event._traceability_product.id}','PUT',
        TraceabilityResutlModel(),hasHeader: true,body: {
          "name": event._traceability_product.name,
          "lot_code": event._traceability_product.lot_code,
          "packing_way": event._traceability_product.packing_way,
          "date_of_manufacture": event._traceability_product.date_of_manufacture,
          "number_of_quarantine_days": event._traceability_product.number_of_quarantine_days,
          "certification": event._traceability_product.certification,
          "description": event._traceability_product.description,
          "traceability_details_attributes[0][traceability_type]": "ingredient",
          "traceability_details_attributes[0][ingredient_name]": event._traceability_product.ingredient.ingredient_name,
          "traceability_details_attributes[0][land_area]": event._traceability_product.ingredient.land_area,
          "traceability_details_attributes[0][ingredient_quantily]": event._traceability_product.ingredient.ingredient_quantily,
          "traceability_details_attributes[0][ingredient_address]": event._traceability_product.ingredient.ingredient_address,
          "traceability_details_attributes[1][manufacture_name]": event._traceability_product.manufactor.manufacture_name,
          "traceability_details_attributes[1][traceability_type]": "manufactor",
          "traceability_details_attributes[1][manufacture_address]": event._traceability_product.manufactor.manufacture_address,
          "traceability_details_attributes[1][manufacture_phone]": event._traceability_product.manufactor.manufacture_phone,
          "traceability_details_attributes[1][manufacture_email]": event._traceability_product.manufactor.manufacture_email,
        },
        realFiles1: event.list_image_product,
        realFiles2: event.list_video_product,
        realFiles3: event.list_image_ingredient,
        realFiles4: event.logo,
        paramFile1: "attachment[file][]",
        paramFile2: "attachment_video[file][]",
        paramFile3: "traceability_details_attributes[0][attachment][file][]",
        paramFile4: "traceability_details_attributes[1][attachment][file][]"
    );
    emit(UpdateTraceabilityState(response));
  });
}