我正在使用 bloc: ^7.0.0 这是目前最新的 bloc 包,现在说听方法现在已被弃用。相反,我应该使用stream.listen。但我不明白如何使用stream.listen。请帮忙解释一下。谢谢!
下面是我的代码:
signInBloc.l?i?s?t?e?n?((state) {
if (state is CheckIfSignedInEventCompletedState) {
if (state.isSignedIn) {
print('logged in');
} else {
print('not logged in');
}
}
Run Code Online (Sandbox Code Playgroud)
class SignInBloc extends Bloc<SignInEvent, SignInState> {
@override
Stream<SignInState> mapEventToState(
SignInEvent event,
) async* {
if (event is CheckIfSignedInEvent) {
yield* mapCheckIfSignedInEventToState();
}
}
Stream<SignInState> mapCheckIfSignedInEventToState() async* {
try {
bool isSignedIn = await authenticationRepository.checkIfSignedIn();
if (isSignedIn != null) {
if (isSignedIn) {
yield CheckIfSignedInEventCompletedState(true);
} else {
yield CheckIfSignedInEventCompletedState(false);
}
} else {
yield CheckIfSignedInEventFailedState();
}
} catch (e) {
print(e);
yield CheckIfSignedInEventFailedState();
}
}
}
Run Code Online (Sandbox Code Playgroud)
signInBloc.l?i?s?t?e?n?((state) {
Run Code Online (Sandbox Code Playgroud)
简单地变成:
signInBloc.stream.listen((state) {
Run Code Online (Sandbox Code Playgroud)
在此处查看从 6.1.x 到 7.0.0 的 bloc 迁移指南:https ://bloclibrary.dev/#/migration?id = packagebloc
| 归档时间: |
|
| 查看次数: |
285 次 |
| 最近记录: |