使用带有 flutter_bloc 的 Equatable 类

Osa*_*med 7 equatable flutter flutter-bloc

为什么当我们需要使用带有 flutter_bloc 的 Equatable 类时?,还有我们需要的道具呢?这是在颤振中以块模式创建状态的示例代码,请我需要详细的答案。先感谢您

abstract class LoginStates extends Equatable{}

class LoginInitialState extends LoginStates{
  @override
  List<Object> get props => [];

}
Run Code Online (Sandbox Code Playgroud)

Can*_*bag 8

我们正在使用 Equatable 包,以便我们可以比较类的实例,而无需手动覆盖“==”和 hashCode。

Equatable 类允许我们比较两个对象是否相等。

这是一个等价的例子。假设我们有以下类:

class Person {
  final String name;

  const Person(this.name);
}
Run Code Online (Sandbox Code Playgroud)

我们可以像这样创建 Person 的实例:

void main() {
  final Person bob = Person("Bob");
}
Run Code Online (Sandbox Code Playgroud)

稍后,如果我们尝试在生产代码或测试中比较 Person 的两个实例,我们将遇到问题。

print(bob == Person("Bob")); // false
Run Code Online (Sandbox Code Playgroud)

为了能够比较 Person 的两个实例,我们需要更改我们的类以覆盖 == 和 hashCode ,如下所示:

class Person {
  final String name;

  const Person(this.name);

  @override
  bool operator ==(Object other) =>
    identical(this, other) ||
    other is Person &&
    runtimeType == other.runtimeType &&
    name == other.name;

  @override
  int get hashCode => name.hashCode;
}
Run Code Online (Sandbox Code Playgroud)

现在,如果我们再次运行以下代码:

print(bob == Person("Bob")); // true
Run Code Online (Sandbox Code Playgroud)

它将能够比较 Person 的不同实例。

因此,当覆盖“==”和 hashCode 时,您不必浪费时间编写大量样板代码。

使用 Equatable 之类的

class Person extends Equatable
Run Code Online (Sandbox Code Playgroud)

在集体案件中;如果您尝试使用具有可变状态的 bloc,您将面临没有 Equatable 的问题。它使资源不可变降低性能。创建副本比改变属性更昂贵。

如果您不清楚我试图解释的内容,阅读本文可能会对您有所帮助。


jit*_*555 5

For comparison of data, we required Equatable. it overrides == and hashCode internally, which saves a lot of boilerplate code. In Bloc, we have to extend Equatable to States and Events classes to use this functionality.

 abstract class LoginStates extends Equatable{}
Run Code Online (Sandbox Code Playgroud)

So, that means LoginStates will not make duplicate calls and will not going to rebuild the widget if the same state occurs.

Define State:

class LoginInitialState extends LoginStates {}
Run Code Online (Sandbox Code Playgroud)

Define State with props:

props declared when we want State to be compared against the values which declared inside props List

class LoginData extends LoginStates {
  final bool status;
  final String userName;
  const LoginData({this.status, this.userName});
  @override
  List<Object> get props => [this.status, this.userName];
}
Run Code Online (Sandbox Code Playgroud)

If we remove the username from the list and keep a list like [this.status], then State will only consider the status field, avoiding the username field. That is why we used props for handling State changes.

Bloc Stream Usage:

As we extending State with Equatable that makes a comparison of old state data with new state data. As an example let's look at the below example here LoginData will build a widget only once, which will avoid the second call as it is duplicated.

@override
Stream<LoginStates> mapEventToState(MyEvent event) async* {
  yield LoginData(true, 'Hello User');
  yield LoginData(true, 'Hello User'); // This will be avoided
}
Run Code Online (Sandbox Code Playgroud)

Detail Blog: https://medium.com/flutterworld/flutter-equatable-its-use-inside-bloc-7d14f3b5479b

  • 上面的答案对于状态来说很清楚,但是对于事件呢?为什么我们需要事件的 equalable?Bloc 允许多次传递同一事件。 (7认同)