Search code examples
flutterdartdart-null-safetyequatable

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?


Solution

  • 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.