【发布时间】:2021-09-12 13:54:29
【问题描述】:
我在我的应用程序中使用 Flutter Bloc 进行状态管理。
当使用 .copyWith() 模式连续产生多个新状态时, 未包含在最新 .copyWith() 调用中的状态成员设置为 null。
这种行为是有意的,还是我的颤振和/或 Bloc 版本太旧了? 我正在使用 Flutter 1.22.6 和 Flutter_bloc 6.1.3
//my_bloc_state.dart
class MyState extends Equatable {
final String foo;
final String bar;
const MyState({this.foo, this.bar});
MyState copyWith({
String foo,
String bar,
}) {
if ((foo == null || identical(foo, this.foo)) &&
(bar == null || identical(bar, this.bar))) {
return this;
}
return MyState(
foo: foo ?? this.foo,
bar: bar ?? this.bar
);
}
@override
List<Object> get props => [foo, bar];
}
//my_bloc.dart
@override
Stream<MyState> mapEventToState(MyEvent event) async* {
if (event is MySpecialEvent) {
yield state.copyWith(foo: "hello");
yield state.copyWith(bar: "World");
print(state.foo) // null
}
}
【问题讨论】:
标签: flutter bloc flutter-bloc