Flutter-使用Freezed实现接口时出错 [英] Flutter - Error implementing interface with Freezed

查看:415
本文介绍了Flutter-使用Freezed实现接口时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用已冻结.我希望能够在我的整个应用程序中指定类型 IUserRegistrationEntity ;

I'm trying to programme to an interface with Freezed. I want to be able to specify all over my app, the type 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;
}

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

Getting the error when running the app:

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;

         ^^^^^^^^

我在做什么错了?

软件包文档中的这句话是否意味着不可能?

Does this quote from the package docs mean it is not possible?.

注2:冻结类不能使用@ With/@ Implements.冻结类既不能扩展也不能实现.

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

渴望知道人们是否认为这是一个缺点.

Keen to know if people think this is a drawback if so.

推荐答案

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

I tested out your code and found the problem from the generated file. Thing is, freezed doesn't override setters from the implemented abstract class. So, for your IUserRegistrationEntity, make the parameters as getters. Like so:

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

这篇关于Flutter-使用Freezed实现接口时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆