copyWith is inherited as a getter #1085
Unanswered
Yogesh-Dubey-Ayesavi
asked this question in
Q&A
Replies: 1 comment 6 replies
-
|
Correct, because now you're inheriting the copyWith from both import 'package:freezed_annotation/freezed_annotation.dart';
part 'message.freezed.dart';
part 'message.g.dart';
enum MessageType { text, payment }
abstract interface class Message {
String get id;
String get authorId;
String get roomId;
Message? get repliedMessage;
MessageType get type;
DateTime get createdAt;
}
@freezed
class TextMessage with _$TextMessage implements Message {
factory TextMessage({
required String id,
required String authorId,
required String roomId,
@MessageConverter() Message? repliedMessage,
@Default(MessageType.text) MessageType type,
required DateTime createdAt,
required String text,
}) = _TextMessage;
const TextMessage._();
factory TextMessage.fromJson(Map<String, dynamic> json) =>
_$TextMessageFromJson(json);
}
class MessageConverter implements JsonConverter<Message, Map<String, dynamic>> {
const MessageConverter();
@override
Message fromJson(Map<String, dynamic> json) {
if (json['type'] == 'text') {
return TextMessage.fromJson(json);
}
throw Exception('Unknown message type');
}
@override
Map<String, dynamic> toJson(Message message) {
if (message is TextMessage) {
return message.toJson();
}
throw Exception('Unknown message type');
}
}Does this work for you? |
Beta Was this translation helpful? Give feedback.
6 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I making message models my script is
I am having a
TextMessageclass which extendsMessagehowever this sayscopyWithis inherited as a getter.can anyone fix this I want the
copyWithfunction.Beta Was this translation helpful? Give feedback.
All reactions