generateImageBase64FromText method
Generates an image from the input prompts and returns it as a base64 string.
Implementation
Future<List<ImageResponse>> generateImageBase64FromText({
required String apiKey,
required String engineId,
required TextToImageRequestParams params,
String? organization,
String? clientId,
String? clientVersion,
}) async {
Map<String, String> headers = {
"Accept": "application/json",
"Authorization": apiKey,
'Content-Type': 'application/json',
"Access-Control-Allow-Origin": "*", // Required for CORS support to work
};
if (organization != null) {
headers.putIfAbsent("Organization", () => organization);
}
if (clientId != null) {
headers.putIfAbsent("Stability-Client-ID", () => clientId);
}
if (clientVersion != null) {
headers.putIfAbsent("Stability-Client-Version", () => clientVersion);
}
Uri endpoint;
if (secure) {
endpoint = Uri.https(baseUrl, "/v1/generation/$engineId/text-to-image");
} else {
endpoint = Uri.http(baseUrl, "/v1/generation/$engineId/text-to-image");
}
var response = await http.post(endpoint,
headers: headers, body: jsonEncode(params.toJson()));
if (response.statusCode == 200 || response.statusCode == 201) {
return ImageResponseListDto.fromJson(
jsonDecode(const Utf8Decoder().convert(response.bodyBytes)))
.artifacts;
} else {
var error = ServerError.fromJson(jsonDecode(response.body));
throw GenerationException(
statusCode: response.statusCode, message: error.message);
}
}