You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter使用Freezed生成文件时遇_privateConstructorUsedError的解决求助

Freezed生成文件出现_privateConstructorUsedError问题求助

我在Flutter应用的Auth模块表单验证中使用Freezed包时,生成的文件抛出了_privateConstructorUsedError错误。运行dart run build_runner build命令后,生成结果和Freezed官方文档及相关教程的预期不符。此前已在GitHub仓库提问但未获回复,现求助是否有开发者遇到过类似问题。

我的代码

import 'package:formz/formz.dart';
import 'package:freezed_annotation/freezed_annotation.dart';

import '../../domain/entities/forms/email_input.dart';
import '../../domain/entities/forms/password_input.dart';

part 'auth_input_validation_state.freezed.dart';

@freezed
class AuthInputValidationState with _$AuthInputValidationState {
const factory AuthInputValidationState({
@default(EmailInput.pure()) EmailInput email,
@default(PasswordInput.pure()) PasswordInput password,
@default(FormzSubmissionStatus.initial) FormzSubmissionStatus status,
}) = _AuthInputValidationState;

factory AuthInputValidationState.fromJson(Map<String, Object?> json) =
_$AuthInputValidationStateFromJson(json);
}

生成的文件内容

// coverage:ignore-file
// GENERATED CODE - DO NOT MODIFY BY HAND
// ignore_for_file: type=lint
// ignore_for_file: unused_element, deprecated_member_use, deprecated_member_use_from_same_package, use_function_type_syntax_for_parameters, unnecessary_const, avoid_init_to_null, invalid_override_different_default_values_named, prefer_expression_function_bodies, annotate_overrides, invalid_annotation_target, unnecessary_question_mark

part of 'auth_input_validation_state.dart';

// **************************************************************************
// FreezedGenerator
// **************************************************************************

T _$identity(T value) => value;

final _privateConstructorUsedError = UnsupportedError(
'It seems like you constructed your class using MyClass._(). This constructor is only meant to be used by freezed and you are not supposed to need it nor use it.\nPlease check the documentation here for more information: https://github.com/rrousselGit/freezed#custom-getters-and-methods');

AuthInputValidationState _$AuthInputValidationStateFromJson(
Map<String, dynamic> json) {
return _AuthInputValidationState.fromJson(json);
}

/// @Nodoc
mixin _$AuthInputValidationState {
EmailInput get email => throw _privateConstructorUsedError;
PasswordInput get password => throw _privateConstructorUsedError;
FormzSubmissionStatus get status => throw _privateConstructorUsedError;

Map<String, dynamic> toJson() => throw _privateConstructorUsedError;
@jsonkey(ignore: true)
$AuthInputValidationStateCopyWith get copyWith =>
throw _privateConstructorUsedError;
}

/// @Nodoc
abstract class $AuthInputValidationStateCopyWith<$Res> {
factory $AuthInputValidationStateCopyWith(AuthInputValidationState value,
$Res Function(AuthInputValidationState) then) =
_$AuthInputValidationStateCopyWithImpl<$Res, AuthInputValidationState>;
@useResult
$Res call(
{EmailInput email, PasswordInput password, FormzSubmissionStatus status});
}

/// @Nodoc
class _$AuthInputValidationStateCopyWithImpl<$Res,
$Val extends AuthInputValidationState>
implements $AuthInputValidationStateCopyWith<$Res> {
_$AuthInputValidationStateCopyWithImpl(this._value, this._then);

// ignore: unused_field
final $Val _value;
// ignore: unused_field
final $Res Function($Val) _then;

@pragma('vm:prefer-inline')
@OverRide
$Res call({
Object? email = null,
Object? password = null,
Object? status = null,
}) {
return _then(_value.copyWith(
email: null == email
? _value.email
: email // ignore: cast_nullable_to_non_nullable
as EmailInput,
password: null == password
? _value.password
: password // ignore: cast_nullable_to_non_nullable
as PasswordInput,
status: null == status
? _value.status
: status // ignore: cast_nullable_to_non_nullable
as FormzSubmissionStatus,
) as $Val);
}
}

/// @Nodoc
abstract class _$$_AuthInputValidationStateCopyWith<$Res>
implements $AuthInputValidationStateCopyWith<$Res> {
factory _$$_AuthInputValidationStateCopyWith(
_$AuthInputValidationState value,
$Res Function($_AuthInputValidationState) then) =
__$$_AuthInputValidationStateCopyWithImpl<$Res>;
@OverRide
@useResult
$Res call(
{EmailInput email, PasswordInput password, FormzSubmissionStatus status});
}

/// @Nodoc
class __$$_AuthInputValidationStateCopyWithImpl<$Res>
extends _$AuthInputValidationStateCopyWithImpl<$Res,
_$_AuthInputValidationState>
implements _$$_AuthInputValidationStateCopyWith<$Res> {
__$$AuthInputValidationStateCopyWithImpl($_AuthInputValidationState value,
$Res Function($_AuthInputValidationState) _then)
: super(_value, _then);

@pragma('vm:prefer-inline')
@OverRide
$Res call({
Object? email = null,
Object? password = null,
Object? status = null,
}) {
return then($_AuthInputValidationState(
email: null == email
? _value.email
: email // ignore: cast_nullable_to_non_nullable
as EmailInput,
password: null == password
? _value.password
: password // ignore: cast_nullable_to_non_nullable
as PasswordInput,
status: null == status
? _value.status
: status // ignore: cast_nullable_to_non_nullable
as FormzSubmissionStatus,
));
}
}

/// @Nodoc
@JsonSerializable()
class _$_AuthInputValidationState implements _AuthInputValidationState {
const _$_AuthInputValidationState({
this.email = const EmailInput.pure(),
this.password = const PasswordInput.pure(),
this.status = FormzSubmissionStatus.initial});

factory _$_AuthInputValidationState.fromJson(Map<String, dynamic> json) =
_$$_AuthInputValidationStateFromJson(json);

@OverRide
@jsonkey()
final EmailInput email;
@OverRide
@jsonkey()
final PasswordInput password;
@OverRide
@jsonkey()
final FormzSubmissionStatus status;

@OverRide
String toString() {
return 'AuthInputValidationState(email: $email, password: $password, status: $status)';
}

@OverRide
bool operator ==(dynamic other) {
return identical(this, other) ||
(other.runtimeType == runtimeType &&
other is _$_AuthInputValidationState &&
(identical(other.email, email) || other.email == email) &&
(identical(other.password, password) ||
other.password == password) &&
(identical(other.status, status) || other.status == status));
}

@jsonkey(ignore: true)
@OverRide
int get hashCode => Object.hash(runtimeType, email, password, status);

@jsonkey(ignore: true)
@OverRide
@pragma('vm:prefer-inline')
_$$AuthInputValidationStateCopyWith<_$_AuthInputValidationState>
get copyWith => __$$_AuthInputValidationStateCopyWithImpl<
_$_AuthInputValidationState>(this, _$identity);

@OverRide
Map<String, dynamic> toJson() {
return _$$_AuthInputValidationStateToJson(
this,
);
}
}

abstract class _AuthInputValidationState implements AuthInputValidationState {
const factory _AuthInputValidationState({
final EmailInput email,
final PasswordInput password,
final FormzSubmissionStatus status}) = _$_AuthInputValidationState;

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

@OverRide
EmailInput get email;
@OverRide
PasswordInput get password;
@OverRide
FormzSubmissionStatus get status;
@OverRide
@jsonkey(ignore: true)
_$$AuthInputValidationStateCopyWith<_$_AuthInputValidationState>
get copyWith => throw _privateConstructorUsedError;
}

内容的提问来源于stack exchange,提问作者Mohammed Jamal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 13:30:56