Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter: Equatable props getter with optional parameter

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?

like image 366
Jacobo Koenig Avatar asked Feb 03 '26 00:02

Jacobo Koenig


1 Answers

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.

like image 194
jamesdlin Avatar answered Feb 04 '26 13:02

jamesdlin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!