save method
- String filePath
Saves the generated image to a file.
@param filePath The path to the file where the image will be saved. @return A Future that resolves to the ImageAi object. @throws StateError if the response is not an http.Response. @throws Exception if there is an error saving the image.
Implementation
Future<ImageAi> save(String filePath) async {
if (response is! http.Response) {
throw StateError('Response is not an http.Response. Cannot save.');
}
file = filePath;
final http.Response res = response; // Use a local variable
final bytes = res.bodyBytes; // Get the bytes
try {
final fileHandle = await File(filePath).create(recursive: true);
await fileHandle.writeAsBytes(bytes);
} catch (e) {
print("Error saving image: $e");
rethrow; // Important: rethrow the error to be caught by the caller.
}
return this;
}