8

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

Community
  • 1
  • 1
Yako
  • 3,405
  • 9
  • 41
  • 71

2 Answers2

7

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

MikeSmith
  • 299
  • 2
  • 6
5

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.

Spencer Stolworthy
  • 1,352
  • 10
  • 17