felangel / equatable

A Dart package that helps to implement value based equality without needing to explicitly override == and hashCode.

Home Page:https://pub.dev/packages/equatable

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add a displayableProps

LefebvreIlyas opened this issue Β· comments

Hello @felangel,
Thank you very much for your work on all the packages. πŸŽ†

Sometimes you need to compare objects on certain fields, but to display others using the toString.

abstract class Equatable {
  List<Object> get props;

  List<Object> get displayableProps => props;
}

Display all the props.
This is the default behavior.

class Foo extends Equatable {
  // Fields

  @override
  List<Object> get props => <Object>[
      foo,
      bar,
      baz,
      foobar,
  ];
}

Display and compare different fields.

class Foo extends Equatable {
  // Fields

  @override
  List<Object> get props => <Object>[
      foo,
      bar,
  ];

  @override
  List<Object> get displayableProps => <Object>[
      baz,
      foobar,
  ];
}

Display all properties except one

class Foo extends Equatable {
  // Fields

  @override
  List<Object> get props => <Object>[
      foo,
      bar,
      baz,
      foobar,
  ];

  @override
  List<Object> get displayableProps => props.removeAt(0);
}

Hi @LefebvreIlyas πŸ‘‹
Thanks for opening an issue!

If you would like custom toString behavior, I would recommend just overriding toString manually for the one-off cases.

class Foo extends Equatable {
  // Fields

  @override
  List<Object> get props => <Object>[
      foo,
      bar,
      baz,
      foobar,
  ];

  @override
  String toString() => 'Foo($bar, $baz, $foobar)`;
}

Hope that helps! Closing for now but feel free to comment with additional comments/questions and I'm happy to continue the conversation πŸ‘