gpt4 book ai didi

Flutter - 使用 Freezed 实现接口(interface)时出错

转载 作者:行者123 更新时间:2023-12-02 02:29:34 25 4
gpt4 key购买 nike

我正在尝试使用 Freezed 对接口(interface)进行编程.我希望能够在我的整个应用程序中指定类型 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;

^^^^^^^^

我做错了什么?

编辑:包文档中的这段引述是否意味着不可能?

Note 2: You cannot use @With/@Implements with freezed classes. Freezedclasses can neither be extended nor implemented.

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

最佳答案

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

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

关于Flutter - 使用 Freezed 实现接口(interface)时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65261500/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com