felangel / equatable

A Dart package that helps to implement value based equality without needing to explicitly override == and hashCode.
https://pub.dev/packages/equatable
MIT License
920 stars 102 forks source link

feat: support stringifyProps #163

Closed devpawann closed 11 months ago

devpawann commented 1 year ago

Status

READY

Breaking Changes

NO

Description

This PR allows user to provide custom stringify props which will be used in toString() method. Before this making stringify=>True; prints all the member which is not required always.

Todos

Steps to Test

class PartialStringifyProps extends Equatable {
  const PartialStringifyProps({
    required this.name,
    required this.age,
  });

  final String name;
  final int age;

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

  @override
  List<Object?>? get stringifyProps => [name];
}

 final instance = PartialStringifyProps(
          name: 'John',
          age: 1,
);
print(instance.toString());
// PartialStringifyProps(John)
felangel commented 11 months ago

I would recommend in those cases for you to explicitly override toString in your class:

class PartialStringifyProps extends Equatable {
  const PartialStringifyProps({
    required this.name,
    required this.age,
  });

  final String name;
  final int age;

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

  @override
  String toString() {
    return 'PartialStringifyProps($name)';
  }
}

 final instance = PartialStringifyProps(
          name: 'John',
          age: 1,
);
print(instance.toString());
// PartialStringifyProps(John)