editImage method

Future<List> editImage(
  1. String imagePath,
  2. String text,
  3. int numberOfVariations
)

Function to edit an image with given text

Implementation

Future<List<dynamic>> editImage(
    String imagePath, String text, int numberOfVariations) async {
  final imageFile = File(imagePath);

  final headers = {'Authorization': 'Bearer $apiKey'};
  final request = http.MultipartRequest('POST', Uri.parse(_editBaseUrl));

  request.headers.addAll(headers);
  request.fields['model'] = 'image-alpha-001';
  request.fields['prompt'] = text;
  request.fields['num_images'] = '$numberOfVariations';
  request.fields['response_format'] = 'url';

  request.files.add(http.MultipartFile.fromBytes(
      'image', imageFile.readAsBytesSync(),
      filename: imageFile.path));

  final response = await request.send();
  final data = await response.stream.bytesToString();

  if (response.statusCode == 200) {
    final success = json.decode(data);
    return success['data'];
  } else {
    final error = json.decode(data);
    throw Exception(error["error"]["message"]);
  }
}