Missing concrete implementation 'getter Equatable' / issue with props
Asked Answered
K

1

5

I am working through many of the tutorials out there on bloc with flutter and running into some inconsistencies.

I am using Android studio and creating the bloc code by using the plugin from Intellij v1.6.0.

For the bloc_event, I continue to see examples that look like this.

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

When I generate my bloc files and look at the initial _event one that generates, it looks like this.

@immutable
abstract class SongEvent extends Equatable {
  const SongEvent();
}

If I modify my code that is generated to include the following...

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

Then I get the following error "Too many positional arguments, 0 expected, 1 found" which references props at the end of the line shown above.

If I leave my code as it was generated by the bloc plugin, and then try to implement my events by adding in the following...

class AddSong extends SongEvent {}

Then I get an error of "Missing concrete implementation of 'getter Equatable.props'

Here is my current bloc/song_event.dart

import 'package:equatable/equatable.dart';
import 'package:meta/meta.dart';

@immutable
abstract class SongEvent extends Equatable {
  const SongEvent();
}

class AddSong extends SongEvent {}

Question Should I be using the line that has props in it as shown in the FuitEvent example?

I don't understand what it is I am missing here and why it shows with an error when I try to use the same method as shown in so many of the tutorials.

Kingsize answered 16/3, 2020 at 6:55 Comment(0)
C
6

Equatable overrides == and hashCode for you so you don't have to waste your time writing lots of boilerplate code.

Basically it helps in doing equality comparison for Objects.

Equatable is an abstract class and it has a getter List<Object> get props;.Concrete Class who extends Equatable must override that getter.

import 'package:equatable/equatable.dart';
import 'package:meta/meta.dart';

@immutable
abstract class SongEvent extends Equatable {
  const SongEvent();
}

class AddSong extends SongEvent {}

In this case, SongEvent is an abstract class, therefore it doesn't have to implement the props getter even tho it extends Equatable. But AddSong is a concrete class that extends SongEvent which extends Equatable, therefore AddSong has to implement the getter in Equatable.

import 'package:equatable/equatable.dart';
import 'package:meta/meta.dart';

@immutable
abstract class SongEvent extends Equatable {
  const SongEvent();
}

// Something like this
class AddSong extends SongEvent {
  final Song song;

  const AddSong(this.song);

  @override
  List<Object> get props => [song];
}
Calve answered 16/3, 2020 at 7:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.