缺少具体的实现"getter平等"/道具问题 [英] Missing concrete implementation 'getter Equatable' / issue with props
问题描述
我正在讨论很多关于blot的教程,其中有些内容扑朔迷离,并且遇到了一些不一致之处.
I am working through many of the tutorials out there on bloc with flutter and running into some inconsistencies.
我正在使用Android Studio,并使用Intellij v1.6.0中的插件来创建块代码.
I am using Android studio and creating the bloc code by using the plugin from Intellij v1.6.0.
对于bloc_event,我继续看到如下示例.
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);
}
当我生成bloc文件并查看生成的初始_event时,它看起来像这样.
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)
然后我得到以下错误位置参数过多,预期有0个,找到了1个",该错误在上一行的末尾引用了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.
如果我将代码保留为由bloc插件生成的代码,然后尝试通过添加以下内容来实现我的事件...
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 {}
然后我得到一个错误:缺少'getter Equatable.props'的具体实现
Then I get an error of "Missing concrete implementation of 'getter Equatable.props'
这是我当前的bloc/song_event.dart
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 {}
问题我应该使用FuitEvent示例中显示的包含道具的那一行吗?
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.
推荐答案
相等的覆盖为==和hashCode为您提供服务,因此您不必浪费时间编写大量样板代码.
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是一个抽象类,它具有一个吸气剂 List< Object>获取道具;
.扩展Equatable的具体类必须重写该getter.
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 {}
在这种情况下,SongEvent是一个抽象类,因此即使扩展了Equatable,也不必实现props getter.但是AddSong是扩展SongEvent并扩展Equatable的具体类,因此AddSong必须在Equatable中实现getter.
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];
}
这篇关于缺少具体的实现"getter平等"/道具问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!