10

I have an object that extends Equatable and contains optional parameters. If I try to add that parameter into the props getter, I get an error The element type 'String?' can't be assigned to the list type 'Object'. However, not adding it would imply equality within objects that have a different value or no value in that parameter.

class Company extends Equatable {
  final String name;
  final String? logo;
....

@override
List<Object> get props {
  return [
    name,
    logo, //error here
....

What would the appropriate solution be?

jamesdlin
  • 81,374
  • 13
  • 159
  • 204
Jacobo Koenig
  • 11,728
  • 9
  • 40
  • 75

2 Answers2

27

The base Equatable.props getter is declared to return a List<Object?>, not List<Object>. Fixing your override to match would allow you to store null.

jamesdlin
  • 81,374
  • 13
  • 159
  • 204
  • Amazing, thanks. I was generating the override with the VSCode quick fix which outputs it without the ? – Jacobo Koenig Aug 15 '21 at 12:07
  • What if the String logo needs to be nullable? I have a similar issue and when I make the class property nullable with a ?, the equatable prop throws the error. How do you solve this? – user3735816 Oct 14 '22 at 17:30
  • @user3735816 As I said, `Equatable.props` should have a return type of `List`, and that already allows you to have nullable properties. What error are you getting? – jamesdlin Oct 14 '22 at 18:32
  • Sorry. Dumb mistake on my part. Your explanation makes perfect sense. – user3735816 Oct 15 '22 at 12:30
0

i suggest to use dynamic instead of Object

Patrick G.
  • 458
  • 5
  • 7