2

我在应用程序中使用@freezed的所有对象都在每个字段中创建 2 个。

我上线了freezed: ^0.12.7

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

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

使用官方文档的复制和粘贴:

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

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

1 回答 1

0

Person从你的代码中得到了一代好对象。part您的陈述可能存在一些错误。每当创建一个新类时,使用@freezewith part 'name.freezed.dart',所以在这种情况下它应该是:

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这是我运行命令后的代码生成:

// 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;
}
于 2021-04-03T02:30:32.027 回答