我正在迁移到新的块8.0.0。我正在尝试删除***mapEventToState***,但我在执行此操作时遇到了困难。您能帮助我如何执行此操作吗?我已在下面尝试过,但它不起作用。
旧代码:
class SignInBloc extends Bloc<SignInEvent, SignInState> {
final AuthenticationRepository authenticationRepository;
final UserDataRepository userDataRepository;
SignInBloc(
{required this.authenticationRepository,
required this.userDataRepository})
: super(SignInInitialState());
SignInState get initialState => SignInInitialState();
@override
Stream<SignInState> mapEventToState(
SignInEvent event,
) async* {
if (event is SignInWithGoogle) {
yield* mapSignInWithGoogleToState();
}
}
Stream<SignInState> mapSignInWithGoogleToState() async* {
yield SignInWithGoogleInProgressState();
try {
String res = await authenticationRepository.signInWithGoogle();
yield SignInWithGoogleCompletedState(res);
} catch (e) {
print(e);
yield SignInWithGoogleFailedState();
}
}
...
迁移代码(不起作用):
class SignInBloc extends Bloc<SignInEvent, SignInState> {
final AuthenticationRepository authenticationRepository;
final UserDataRepository userDataRepository;
SignInBloc(
{required this.authenticationRepository,
required this.userDataRepository})
: super(SignInInitialState())
{
SignInState get initialState => SignInInitialState();
on<SignInWithGoogle>((event, emit) => emit(mapSignInWithGoogleToState()));
}
Stream<SignInState> mapSignInWithGoogleToState() async* {
yield SignInWithGoogleInProgressState();
try {
String res = await authenticationRepository.signInWithGoogle();
yield SignInWithGoogleCompletedState(res);
} catch (e) {
print(e);
yield SignInWithGoogleFailedState();
}
}
...
2条答案
按热度按时间oprakyz71#
您的问题是
mapSignInWithGoogleToState()
返回了一个Stream
的状态,但是每次需要发出一个新的状态时,新结构都需要显式调用emit
。下面是关于“为什么"的一些更多信息:https://bloclibrary.dev/#/migration?id=rationale-8
ivqmmu1c2#
getter不属于构造函数体,我想它也不再需要了。你可以这样解决这个问题:
注意,bloc v8中的事件不再是按顺序处理的,而是并发处理的。https://verygood.ventures/blog/how-to-use-bloc-with-streams-and-concurrency