0

我正在尝试使用Freezed对接口进行编程。我希望能够在我的应用程序中指定类型IUserRegistrationEntity

我的界面:

abstract class IUserRegistrationEntity {
  String nickName;
  String email;
  String confirmEmail;
  String password;
  String confirmPassword;
}

我的冻结班:

import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:vepo/domain/user_registration/i_user_registration_entity.dart';

part 'user_registration_entity.freezed.dart';

@freezed
abstract class UserRegistrationEntity with _$UserRegistrationEntity {
  @Implements(IUserRegistrationEntity)
  factory UserRegistrationEntity(
      {String nickName,
      String email,
      String confirmEmail,
      String password,
      String confirmPassword}) = _UserRegistrationEntity;
}

运行应用程序时出现错误:

lib/domain/user_registration/user_registration_entity.freezed.dart:165:7: Error: The non-abstract class '_$_UserRegistrationEntity' is missing implementations for these members:
 - IUserRegistrationEntity.confirmEmail
 - IUserRegistrationEntity.confirmPassword
 - IUserRegistrationEntity.email
 - IUserRegistrationEntity.nickName
 - IUserRegistrationEntity.password
Try to either
 - provide an implementation,
 - inherit an implementation from a superclass or mixin,
 - mark the class as abstract, or
 - provide a 'noSuchMethod' implementation.

class _$_UserRegistrationEntity implements _UserRegistrationEntity {
      ^^^^^^^^^^^^^^^^^^^^^^^^^
lib/domain/user_registration/i_user_registration_entity.dart:4:10: Context: 'IUserRegistrationEntity.confirmEmail' is defined here.
  String confirmEmail;

         ^^^^^^^^^^^^
lib/domain/user_registration/i_user_registration_entity.dart:6:10: Context: 'IUserRegistrationEntity.confirmPassword' is defined here.
  String confirmPassword;
         ^^^^^^^^^^^^^^^
lib/domain/user_registration/i_user_registration_entity.dart:3:10: Context: 'IUserRegistrationEntity.email' is defined here.
  String email;
         ^^^^^
lib/domain/user_registration/i_user_registration_entity.dart:2:10: Context: 'IUserRegistrationEntity.nickName' is defined here.
  String nickName;
         ^^^^^^^^

lib/domain/user_registration/i_user_registration_entity.dart:5:10: Context: 'IUserRegistrationEntity.password' is defined here.
  String password;

         ^^^^^^^^

我究竟做错了什么?

编辑:包文档中的这句话是否意味着不可能?

注意 2:您不能将 @With/@Implements 与冻结的类一起使用。冻结的类既不能扩展也不能实现。

很想知道人们是否认为这是一个缺点。

4

3 回答 3

2

我使用了 rkdupr0n 的解决方案。我只想说,我还让IUserRegistrationEntity接口知道了 Freezed 包的功能,以便在对IUserRegistrationEntity接口进行编程时可以调用它们。好吧,只是我目前需要的那些。我会尽快添加其余的。

冻结类变为:

import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:vepo/domain/user_registration/i_user_registration_entity.dart';

part 'user_registration_entity.freezed.dart';
part 'user_registration_entity.g.dart';

@freezed
abstract class UserRegistrationEntity with _$UserRegistrationEntity {
  @Implements.fromString(
      'IUserRegistrationEntity<\$UserRegistrationEntityCopyWith<IUserRegistrationEntity>>')
  const factory UserRegistrationEntity(
      {String nickName,
      String email,
      String confirmEmail,
      String password,
      String confirmPassword}) = _IUserRegistrationEntity;

  factory UserRegistrationEntity.fromJson(Map<String, dynamic> json) =>
      _$UserRegistrationEntityFromJson(json);
}

界面:

abstract class IUserRegistrationEntity<T> extends FreezedClass<T> {
  String get nickName;
  String get email;
  String get confirmEmail;
  String get password;
  String get confirmPassword;
}

abstract class FreezedClass<T> {
  T get copyWith;
  Map<String, dynamic> toJson();
}
于 2020-12-12T11:03:42.320 回答
2

我测试了您的代码,并从生成的文件中发现了问题。问题是, freezed 不会覆盖已实现抽象类的设置器。因此,对于您的IUserRegistrationEntity,将参数设置为 getter。像这样:

abstract class IUserRegistrationEntity {
  String get nickName;
  String get email;
  String get confirmEmail;
  String get password;
  String get confirmPassword;
}
于 2020-12-12T08:19:05.847 回答
-1

添加私有空构造函数代码

例如,如果您正在生成冻结的用户类

@freezed
abstract class User with _$User {
  const User._(); // add this
}

它看起来像那样

于 2021-10-06T16:23:35.400 回答