Skip to content

Getters are included in generated mapping #84

Description

@petrnymsa

Often classes contains getters without explicit setter (computed getter,...). In such case, generated code tries to assign value to them.

Run smartstruct 1.4.0 against this mapper:

class GetterTarget {
  final String name;
  final int age;

  int foo = 0;

  GetterTarget({
    required this.name,
    required this.age,
  });

  List<Object?> get props => [name, age];

  bool get sample => false;
}

class GetterSource {
  final String name;
  final int age;

  int foo = 1;

  GetterSource({
    required this.name,
    required this.age,
  });

  List<Object?> get props => [name, age];

  bool get sample => true;
}

@Mapper()
abstract class GetterMapper {
  GetterTarget fromModel(GetterSource source);
}

Generated mapping

class GetterMapperImpl extends GetterMapper {
  GetterMapperImpl() : super();

  @override
  GetterTarget fromModel(GetterSource source) {
    final gettertarget = GetterTarget(
      name: source.name,
      age: source.age,
    );
    gettertarget.foo = source.foo;
     // NO SETTER !
    gettertarget.props = source.props.map((e) => e).toList();

   // NO SETTER !
    gettertarget.sample = source.sample;
    return gettertarget;
  }
}

This can happen alot if project uses Equatable, where props, stringify and hashCode is used.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions