createAsset method
Implementation
void createAsset(
BuildContext context, TinyImage tinyImage, int insertionId) async {
if (productName.text.isEmpty || productDetails.text.isEmpty) {
return Utils.showSnackbar(context, Strings.error,
'Product Name and Detail are required!', AppColors.red);
} else {
var uri = Uri.parse("${await Constants.baseUrl()}/merchant/assets/create");
final mimeTypeData =
lookupMimeType(tinyImage.picturePath!, headerBytes: [0xFF, 0xD8])!
.split('/');
// Intilize the multipart request
final imageUploadRequest = http.MultipartRequest('POST', uri);
final file = await http.MultipartFile.fromPath(
'file', tinyImage.picturePath!,
contentType: MediaType(mimeTypeData[0], mimeTypeData[1]));
imageUploadRequest.files.add(file);
imageUploadRequest.fields['merchant_id'] =
Storage.getValue(Constants.merchantID);
imageUploadRequest.fields['api_key'] = await Constants.apiKey();
imageUploadRequest.fields['name'] = productName.text.toString();
imageUploadRequest.fields['description'] = productDetails.text.toString();
try {
final streamedResponse = await imageUploadRequest.send();
final response = await http.Response.fromStream(streamedResponse);
var jsonString = response.body;
AssetsResponse assetsResponse =
AssetsResponse.fromJson(json.decode(jsonString));
if (assetsResponse.status == Strings.success) {
selectedImagePaths.insert(
insertionId, assetsResponse.data!.id.toString());
// print('total in the selected is ${selectedImagePaths}');
} else {
return Utils.showSnackbar(
context,
Strings.error,
json.decode(jsonString)['message'].toString().toTitleCase(),
AppColors.red);
}
} catch (e) {
Utils.showSnackbar(context, Strings.error, e.toString(), AppColors.red);
}
update();
}
}