uploadImage method

Future<String> uploadImage(
  1. File image
)

Implementation

Future<String> uploadImage(File image) async {
  final request = http.MultipartRequest(
    'POST',
    Uri.parse('https://app-haiva.gateway.apiplatform.io/v1/insertAvatar'),
  )
    ..headers['Authorization'] = 'Bearer ${Constants.accessToken}' // Add the authorization header
    ..fields['workspaceId'] = Constants.workspaceId! // Add the workspaceId field
    ..files.add(
      http.MultipartFile.fromBytes(
        'avatarFile',
        await image.readAsBytes(),
        filename: image.path.split('/').last,
        contentType: MediaType.parse(lookupMimeType(image.path) ?? 'application/octet-stream'),
      ),
    );

  final response = await request.send();
  if (response.statusCode == 200 || response.statusCode == 201) {
    final responseData = await response.stream.bytesToString();
    final Map<String, dynamic> data = json.decode(responseData);
    if (data['Url'] != null && data['Url'] is String) {
      print('Url: ${data['Url']}');
      return data['Url'] as String;
    } else {
      throw Exception('Invalid response format: Url is missing or not a string');
    }
  } else {
    final responseData = await response.stream.bytesToString();
    final errorMessage = json.decode(responseData)['message'] ?? 'Failed to upload image';
    throw Exception(errorMessage);
  }
}