3

I've been following this NoSQL tutorial which uses BLoC. But when I paste in the code from 'fruit_event.dart' into Visual Studio Code, it gives me an error.

fruit_event.dart:

import 'package:equatable/equatable.dart';
import 'package:meta/meta.dart';
import 'package:sembast_prep/data/fruit.dart';

@immutable
abstract class FruitEvent extends Equatable {
  FruitEvent([List props = const []]) : super(props); // error here!
}

class LoadFruits extends FruitEvent {}

class AddRandomFruit extends FruitEvent {}

class UpdateWithRandomFruit extends FruitEvent {
  final Fruit updatedFruit;

  UpdateWithRandomFruit(this.updatedFruit) : super([updatedFruit]);
}

class DeleteFruit extends FruitEvent {
  final Fruit fruit;

  DeleteFruit(this.fruit) : super([fruit]);
}

I get this error (Visual Studio Code):

[List<dynamic> props = const []]
Too many positional arguments: 0 expected, but 1 found.
Try removing the extra positional arguments.dart(extra_positional_arguments)
JakesMD
  • 1,646
  • 2
  • 15
  • 35

1 Answers1

10

Source code of Equatable class: https://github.com/felangel/equatable/blob/master/lib/src/equatable.dart

It has only one constructor and the constructor accepts no argument/parameter. So you are not allowed to call super(props) from your constructor.

But, in your code you are passing one argument i.e props to the constructor of super class i.e Equatable in your case.

Replace this:

FruitEvent([List props = const []]) : super(props);

With this:

FruitEvent([List props = const []]);

And it won't give you any error.

You may want to refer this question to understand usage of super constructor in dart: How do I call a super constructor in Dart?

Edited:

  1. You should use Equitable class to override your hashcode and toString.
  2. so for the fields which you want to include in your hashcode and toString you need to have following override:

/// The [List] of `props` (properties) which will be used to determine whether
/// two [Equatables] are equal.
@override
List<Object> get props => [parameter1, parameter2, parameter3.....];

  1. But as you don't have any fields in your class it doesn't makes much sense to use Equitable.

Read this comment from the source code:

/// A class that helps implement equality
/// without needing to explicitly override == and [hashCode].
/// Equatables override their own `==` operator and [hashCode] based on their `props`.
const Equatable();

You can refer: https://pub.dev/packages/equatable#-example-tab- to learn about using Equitable class.

I hope this helps, in case of any doubt please comment. If this answer helps you then please accept and up-vote it.

Kalpesh Kundanani
  • 5,413
  • 4
  • 22
  • 32