AttachmentContent.fromJson constructor
AttachmentContent.fromJson(
- Map<String, dynamic> json
)
Implementation
factory AttachmentContent.fromJson(Map<String, dynamic> json) {
// Initialize with empty data as default
Uint8List dataBytes = Uint8List(0);
// Process data field based on its type
if (json.containsKey('data')) {
var data = json['data'];
if (data is String) {
try {
// First try base64 decoding
dataBytes = base64Decode(data);
} catch (e) {
print('Error base64 decoding data: $e');
// If base64 fails, try hex decoding if it looks like hex
if (RegExp(r'^[0-9a-fA-F]+$').hasMatch(data)) {
try {
dataBytes = Uint8List.fromList(List<int>.generate(data.length ~/ 2, (i) => int.parse(data.substring(i * 2, i * 2 + 2), radix: 16)));
} catch (e) {
print('Error hex decoding data: $e');
}
}
}
} else if (data is List) {
// Handle list of integers
dataBytes = Uint8List.fromList(List<int>.from(data));
} else if (data is Uint8List) {
// Already in correct format
dataBytes = data;
}
}
return AttachmentContent(
filename: json['filename'] as String? ?? 'attachment',
mimeType: json['mimeType'] as String? ?? 'application/octet-stream',
data: dataBytes,
description: json['description'] as String?,
);
}