Flutter: Equatable props getter with optional parameter

577

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.

Share:
577
Jacobo Koenig
Author by

Jacobo Koenig

Updated on December 31, 2022

Comments

  • Jacobo Koenig
    Jacobo Koenig over 1 year

    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?

  • Jacobo Koenig
    Jacobo Koenig almost 3 years
    Amazing, thanks. I was generating the override with the VSCode quick fix which outputs it without the ?