如何在用“冻结”库注释的类中添加自定义函数?

am5*_*a03 9 dart flutter

我定义了一个这样的类,并用冻结的库进行了注释。

@freezed
@immutable
abstract class CommentMediaAttachmentModel with _$CommentMediaAttachmentModel {
  const factory CommentMediaAttachmentModel({
    final String type,
    final String mediaUrl,
    final int width,
    final int height
  }) = _CommentMediaAttachmentModel;

  bool isAnimated() {
    return type == 'ANIMATED';
  } 
}
Run Code Online (Sandbox Code Playgroud)

我想添加一个快速函数isAnimated来确定type变量,但在编译时,它不允许我这样做:

lib/presentation/comment/model/comment_attachment_model.freezed.dart:292:7: Error: The non-abstract class '_$_CommentMediaAttachmentModel' is missing implementations for these members:
 - CommentMediaAttachmentModel.isAnimated
Try to either
 - provide an implementation,
 - inherit an implementation from a superclass or mixin,
 - mark the class as abstract, or
 - provide a 'noSuchMethod' implementation.
Run Code Online (Sandbox Code Playgroud)

检查生成的类后_$_CommentMediaAttachmentModelisAnimated函数未实现。我怎样才能做到这一点?

编辑:下面是 的代码_$_CommentMediaAttachmentModel。我不知道为什么我不能将该片段粘贴到 SO,它只是说代码格式错误。我将使用屏幕截图来代替: 在此输入图像描述

THe*_*ere 25

要在类上手动定义方法/属性,如冻结文档中所述,您必须定义一个私有构造函数:

@freezed
@immutable
abstract class CommentMediaAttachmentModel with _$CommentMediaAttachmentModel {
  const CommentMediaAttachmentModel._(); // Added constructor
  const factory CommentMediaAttachmentModel({
    final String type,
    final String mediaUrl,
    final int width,
    final int height
  }) = _CommentMediaAttachmentModel;

  bool isAnimated() {
    return type == 'ANIMATED';
  } 
}
Run Code Online (Sandbox Code Playgroud)