首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >颤振冻结对象每个字段中有两个

颤振冻结对象每个字段中有两个
EN

Stack Overflow用户
提问于 2021-04-03 00:31:30
回答 1查看 1.8K关注 0票数 2

应用程序中所有使用@冻结的对象都在创建每个字段中的两个。

我在freezed: ^0.12.7

当我从0.12.7版本的文档复制粘贴示例时:

代码语言:javascript
复制
import 'package:freezed_annotation/freezed_annotation.dart';

part 'x.freezed.dart';

@freezed
abstract class Person implements _$Person {
  // uses implements instead of with
  const Person._(); // Added constructor
  const factory Person(String name, {int age}) = _Person;

  void method() {
    print('hello world');
  }
}

重复出现如下情况:

编辑:,我刚刚在freezed 0.14.1+2中确认了完全相同的行为。

从官方文档中复制和粘贴:

代码语言:javascript
复制
@freezed
class Person with _$Person {
  const Person._(); // Added constructor
  const factory Person(String name, {int? age}) = _Person;

  void method() {
    print('hello world');
  }
}
EN

回答 1

Stack Overflow用户

发布于 2021-04-03 02:30:32

我从您的代码中获得了很好的一代Person对象。可能有一些与part语句相关的错误。每当创建一个新类时,都可以将@freezepart 'name.freezed.dart'一起使用,因此在本例中应该是:

代码语言:javascript
复制
import 'package:freezed_annotation/freezed_annotation.dart';

part 'person.freezed.dart';

@freezed
abstract class Person implements _$Person {
  // uses implements instead of with
  const Person._(); // Added constructor
  const factory Person(String name, {int age}) = _Person;

  void method() {
    print('hello world');
  }
}

下面是运行flutter pub run build_runner build命令后的代码生成:

代码语言:javascript
复制
// GENERATED CODE - DO NOT MODIFY BY HAND
// ignore_for_file: 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

part of 'person.dart';

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

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

/// @nodoc
class _$PersonTearOff {
  const _$PersonTearOff();

// ignore: unused_element
  _Person call(String name, {int age}) {
    return _Person(
      name,
      age: age,
    );
  }
}

/// @nodoc
// ignore: unused_element
const $Person = _$PersonTearOff();

/// @nodoc
mixin _$Person {
  String get name;
  int get age;

  @JsonKey(ignore: true)
  $PersonCopyWith<Person> get copyWith;
}

/// @nodoc
abstract class $PersonCopyWith<$Res> {
  factory $PersonCopyWith(Person value, $Res Function(Person) then) =
      _$PersonCopyWithImpl<$Res>;
  $Res call({String name, int age});
}

/// @nodoc
class _$PersonCopyWithImpl<$Res> implements $PersonCopyWith<$Res> {
  _$PersonCopyWithImpl(this._value, this._then);

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

  @override
  $Res call({
    Object name = freezed,
    Object age = freezed,
  }) {
    return _then(_value.copyWith(
      name: name == freezed ? _value.name : name as String,
      age: age == freezed ? _value.age : age as int,
    ));
  }
}

/// @nodoc
abstract class _$PersonCopyWith<$Res> implements $PersonCopyWith<$Res> {
  factory _$PersonCopyWith(_Person value, $Res Function(_Person) then) =
      __$PersonCopyWithImpl<$Res>;
  @override
  $Res call({String name, int age});
}

/// @nodoc
class __$PersonCopyWithImpl<$Res> extends _$PersonCopyWithImpl<$Res>
    implements _$PersonCopyWith<$Res> {
  __$PersonCopyWithImpl(_Person _value, $Res Function(_Person) _then)
      : super(_value, (v) => _then(v as _Person));

  @override
  _Person get _value => super._value as _Person;

  @override
  $Res call({
    Object name = freezed,
    Object age = freezed,
  }) {
    return _then(_Person(
      name == freezed ? _value.name : name as String,
      age: age == freezed ? _value.age : age as int,
    ));
  }
}

/// @nodoc
class _$_Person extends _Person {
  const _$_Person(this.name, {this.age})
      : assert(name != null),
        super._();

  @override
  final String name;
  @override
  final int age;

  @override
  String toString() {
    return 'Person(name: $name, age: $age)';
  }

  @override
  bool operator ==(dynamic other) {
    return identical(this, other) ||
        (other is _Person &&
            (identical(other.name, name) ||
                const DeepCollectionEquality().equals(other.name, name)) &&
            (identical(other.age, age) ||
                const DeepCollectionEquality().equals(other.age, age)));
  }

  @override
  int get hashCode =>
      runtimeType.hashCode ^
      const DeepCollectionEquality().hash(name) ^
      const DeepCollectionEquality().hash(age);

  @JsonKey(ignore: true)
  @override
  _$PersonCopyWith<_Person> get copyWith =>
      __$PersonCopyWithImpl<_Person>(this, _$identity);
}

abstract class _Person extends Person {
  const _Person._() : super._();
  const factory _Person(String name, {int age}) = _$_Person;

  @override
  String get name;
  @override
  int get age;
  @override
  @JsonKey(ignore: true)
  _$PersonCopyWith<_Person> get copyWith;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66926695

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档