Im making a flutter app from an legacy app made in JS.
the legacy app sends pictures that way:
async createNewMaintenanceService(data: {
areaType: MaintenanceArea;
type: MaintenanceType;
comments: string;
area?: Area;
photo?: Blob;
subcategory?: IssueType;
period?: MaintenancePeriod;
unattendedAccessAllowed?: boolean;
}): Promise<any> {
console.log('TCL: MaintenanceService -> constructor -> data', data);
const formData = new FormData();
formData.append('areaType', data.areaType);
formData.append('comments', data.comments);
formData.append('type', data.type);
if (data.area) {
formData.append('area', String(data.area.id));
}
if (data.photo) {
formData.append('file', data.photo);
}
if (data.subcategory) {
formData.append('subcategory', String(data.subcategory.id));
}
if (data.period) {
formData.append('suggestedServicePeriod', data.period);
}
if (data.unattendedAccessAllowed) {
formData.append('unattendedAccessAllowed', String(data.unattendedAccessAllowed ? 1 : 0));
}
console.log('TCL: MaintenanceService -> constructor -> formData', formData);
return await this.API.post('/maintenances', formData, {
tokenRequired: true,
});
}
In flutter, im doing it like that:
// *** state.imageFile is a File()***
// *** this is the data i'm sending :
final data = {
'areaType': area,
'comments': commentController.text,
'period': period,
'AccessAllowed': isAuthorizedAccess,
'file': imageFile, // here im having problems
}..removeWhere((key, value) => value == null);
Future<Either<CreateMaintenanceException, dynamic>> createNewMaintenance(
Map<String, dynamic> data,
) async {
try {
final result = await _client.post(
isTokenRequired: true,
path: '/maintenances',
data: data,
);
return Right(
result,
);
} on DioException catch (e) {
return Left(
CreateMaintenanceException(e.response?.data),
);
}
}
I can do the req if i send image as null (not sending), but i'm having problems sending that imageFile.
I already tried to convert imageFile and send inside data
that way:
Future<String> toBase64() async {
final path = this.path;
final List<int> imageBytes = await File(path).readAsBytes();
return base64Encode(imageBytes);
}
imageFile!.toBase64()
and like that:
Future<List<int>> toBlob() async {
final path = this.path;
final List<int> imageBytes = await File(path).readAsBytes();
return imageBytes;
}
imageFile!.toBlob()
but no one worked.
What am I doing wrong?