Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
File size: 2,290 Bytes
5acd9c3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
// lib/models/chat_message.dart
import 'package:uuid/uuid.dart';
class ChatMessage {
final String id;
final String userId;
final String username;
final String content;
final DateTime timestamp;
final String videoId;
final String? color;
ChatMessage({
String? id,
required this.userId,
required this.username,
required this.content,
required this.videoId,
this.color,
DateTime? timestamp,
}) : id = id ?? const Uuid().v4(),
timestamp = timestamp ?? DateTime.now();
factory ChatMessage.fromJson(Map<String, dynamic> json) {
// Handle potential null or missing values
final String? id = json['id'] as String?;
final String? userId = json['userId'] as String?;
final String? username = json['username'] as String?;
final String? content = json['content'] as String?;
final String? videoId = json['videoId'] as String?;
final String? color = json['color'] as String?;
// Validate required fields
if (userId == null || username == null || content == null || videoId == null) {
throw FormatException(
'Invalid chat message format. Required fields missing: ${[
if (userId == null) 'userId',
if (username == null) 'username',
if (content == null) 'content',
if (videoId == null) 'videoId',
].join(', ')}'
);
}
// Parse timestamp with fallback
DateTime? timestamp;
final timestampStr = json['timestamp'] as String?;
if (timestampStr != null) {
try {
timestamp = DateTime.parse(timestampStr);
} catch (e) {
print('Error parsing timestamp: $e');
// Use current time as fallback
timestamp = DateTime.now();
}
}
return ChatMessage(
id: id,
userId: userId,
username: username,
content: content,
videoId: videoId,
color: color,
timestamp: timestamp,
);
}
Map<String, dynamic> toJson() => {
'id': id,
'userId': userId,
'username': username,
'content': content,
'videoId': videoId,
'color': color,
'timestamp': timestamp.toIso8601String(),
};
@override
String toString() => 'ChatMessage(id: $id, userId: $userId, username: $username, content: $content, videoId: $videoId)';
} |