Passing properties to the super constructor of Equatable

1,117

Solution 1

The official Equatable docs describe how to expose your comparison properties to the super class. You actually don't need to call super in your constructor at all. Instead, you are going to use code like the following (not my code, taken from the docs):

class Person extends Equatable {
  final String name;

  Person(this.name);

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

The key here is to override the props getter. The equatable super class looks to the properties in the props getter to do its magic.

All equatable does is override the == operator in your classes. There is an excellent medium article that goes over some common operator overrides that you may find useful.

Solution 2

I might be a bit late to the party. But can advise the following - you don't need to pass any variables through to the Equatable super constructor. Instead, you can override the Equatable base class from within your Failure class and assign "props" to an empty array. Any class that inherits Failure can also "@override" props if needed.

import 'package:equatable/equatable.dart';

abstract class Failure extends Equatable {
  @override
  List<Object> get props => [];
}

Kia Kaha,

Mike Smith

Share:
1,117
Yako
Author by

Yako

I'm mainly a UX Designer, user researcher, interface designer. But I also build up some stuff for web and mobile with HTML5, jQuery, PHP, and some pinches of a few frameworks.

Updated on December 17, 2022

Comments

  • Yako
    Yako over 1 year

    I'm quite new to Flutter and Dart, and I have some troubles understanding how to rewrite a class extending an updated version of Equatable.

    This works with Equatable 0.4.0:

    abstract class Failure extends Equatable {
       Failure([List properties = const<dynamic>[]]) : super(properties);
    }
    

    However, updating to Equatable 1.0.2 throws an error at super(properties):

    Too many positional arguments: 0 expected, but 1 found.

    Try removing the extra arguments.

    I don't understand how to pass over properties to the super constructor with Equatable 1.0.2