首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MIgrating使用多个enevts和共享首选项锁定>= 7.2.0

MIgrating使用多个enevts和共享首选项锁定>= 7.2.0
EN

Stack Overflow用户
提问于 2022-02-28 11:24:22
回答 1查看 75关注 0票数 0

我正在开发一个用预>=7.2.0阵营版本创建的登录页面,我在迁移这个AuthBloc时遇到了问题,因为它有多个事件和共享的首选项。

代码语言:javascript
复制
class AuthBloc extends Bloc<AuthEvent, AuthStates> {
  AuthBloc() : super(Initialization());

  Stream<AuthStates> mapEventToState(AuthEvent event) async* {
    yield WaitingAuth();
    switch (event.runtimeType) {
      case InitEvent:
        SharedPreferences prefs = await SharedPreferences.getInstance();
        bool login = prefs.getBool('login');
        if (login == null || !login) {
          prefs.clear();
          yield Initialization();
          break;
        } else {
          String token = prefs.getString('token');
          String tokenJWT = prefs.getString('tokenJWT');
          if (token == null ||
              tokenJWT == null ||
              token.isEmpty ||
              tokenJWT.isEmpty) {
            yield Initialization();
          } else {
            setToken(token);
            setJWTToken(tokenJWT);
            final response = await Api.getAccount();
            if (response is Account) {
              final sensorResponse = await Api.getDevices();
              if (sensorResponse is List<Sensor>) {
                yield SuccessAuth(account: response, sensors: sensorResponse);
              } else {
                yield SuccessAuth(account: response, sensors: []);
              }
            } else {
              yield Initialization();
            }
          }
        }break;
      default:
        SentryCapture.error(
            loggerName: 'AuthBloc',
            environment: 'switch',
            message: 'unhandled event($event)');
    }
  }
}

我该怎么做?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-02-28 11:37:32

对于颤振组>= 7.2.0,您必须使用新的on< Event> API并将yield替换为emit。下面是一个小例子。

代码语言:javascript
复制
MyBloc() : super (MyInitialState()) {
 on<MyEvent1>((event, emit) => emit(MyState1()));
 on<MyEvent2>((event, emit) => emit(MyState2()));
}

对于您的情况,请执行以下操作。

代码语言:javascript
复制
AuthBloc() : super(Initialization()) {
 on<AuthEvent>((event, emit) {
  emit(WaitingAuth());
  // Your logic
 }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71294293

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档