import 'dart:convert'; enum ImMessageType { chatPrivate('CHAT_PRIVATE'), chatGroup('CHAT_GROUP'), online('ONLINE'), heartbeat('HEARTBEAT'), system('SYSTEM'), notice('NOTICE'); final String value; const ImMessageType(this.value); } class ImMessage { final ImMessageType type; final String title; final String from; final String to; final String content; ImMessage({ required this.type, required this.title, required this.from, required this.to, required this.content, }); factory ImMessage.fromJsonString(String json) { return ImMessage.fromJson(jsonDecode(json)); } factory ImMessage.fromJson(Map json) { return ImMessage( type: ImMessageType.values.firstWhere((e) => e.value == json['type']), title: json['title'], from: json['from'], to: json['to'], content: json['content'], ); } Map toJson() => { 'type': type.value, 'title': title, 'from': from, 'to': to, 'content': content, }; String toJsonString() { return jsonEncode(toJson()); } static ImMessage online(int userId) { return ImMessage( type: ImMessageType.online, title: '', from: userId.toString(), to: 'SYSTEM', content: '', ); } }