Skip to content

Problem in using ReactiveImagePicker (in conversion of model object to/from form.value) #125

@rsbichkar

Description

@rsbichkar

I am using ReactiveForms along with ReactiveImagePicker and several other Reactive packages for the implementation of an application involving several forms. I am also using Freezed, JsonSerialization, and Riverpod packages and AppWrite for backend.

In ReactiveForms using ReactiveImagePicker, I am unable to pass 'PersonName' model class object to Reactive Form widget's form.value (for the editing mode) due to use of different types in model class and the form group (String? type in model class and List<SelectedFile> type in form group) for the field using ReactiveImagePicker.

Extract of related code in my app is given below to understand the exact problem:

The model class file (PersonName) contains other fields besides profilePic, of type String?, which is used to store the id or Url of image file stored in Appwrite Storage bucket:

class PersonName with _$PersonName {
  const factory PersonName({
    @JsonKey(name: '\$id') String? id,
    required String name,
    ... // other fields 
    String? profilePic,  // this field stores the id or Url of image stored in Appwrite Storage bucket
  }) = _PersonName;

The FormGroup for this class is like this:

final form = FormGroup({
    'name': FormControl<String>(
      validators: [Validators.required],
    ),
    ... // other entries
    'profilePic': FormControl<List<SelectedFile>>(),
  });
}

The main problem is how to convert PersonName object to form.value data (in form display widget) as the form.value has field of type List<SelectedFile> whereas PersonName has the corresponding model class field is of type String?.

For this I am using PersonNameToFormValue (and corresponding PersonNameFromFormValue) functions:

Map<String, dynamic> PersonNameToFormValue(PersonName personName) {
  final map = <String, dynamic>{
    'id': personName.id,
    'name': personName.name,
      ... //other entries
    'profilePic': personName.profilePic != null
            ? [SelectedFileImage(file: File(personName.profilePic!))]   // File is from dart:io package
            : [SelectedFileImage(url: personName.profilePic!)],
  };
  return map;
}

'profilePic' field in above function uses condition to pass either a File or Url to display widget image in either read only or editing mode.

The line using 'url:' displays the image correctly in readonly mode. However, the line using 'file:' gives the following error:

════════ Exception caught by image resource service ════════════════════════════
The following PathNotFoundException was thrown resolving an image codec:
Cannot retrieve length of file, path = 'http://192.168.12.203:4003/v1/storage/buckets/65092e956108ccf06e17/files/6509e7cde12e9b138dc4/view?project=64f436798e981511b356&mode=admin' (OS Error: No such file or directory, errno = 2)

Will you please help in this regard?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions