【问题标题】:BlocListener Only returning intial loading stateBlocListener 只返回初始加载状态
【发布时间】:2021-08-28 21:14:14
【问题描述】:

我正在用颤振块构建一个应用程序。我遇到的问题是我的 bloc 侦听器仅触发初始状态而不是后续状态更改。由于我的状态扩展为可比较状态,所有其他问题都没有帮助。下面是我的代码;

我的登录区

import 'package:bloc/bloc.dart';
import 'package:mobile_app/classes/custom_exception.dart';
import 'package:mobile_app/repositories/auth_repository.dart';
import 'package:mobile_app/states/login_status.dart';

class LoginBloc extends Cubit<LoginState> {
   LoginBloc(this.auth) : super(LoginState.initial());

   final AuthRepository auth;

   void login(String email, String password) async {
      emit(state.copyWith(loginStatus: Status.LOADING, isAuthenticated: false));
      final response = await auth.doLogin(email, password);
      if (response is AppException) {
        emit(state.copyWith(
           loginStatus: Status.ERROR,
           error: response.toString(),
           isAuthenticated: false));
     } else {
       emit(
          state.copyWith(loginStatus: Status.COMPLETED, isAuthenticated: true));
     }
  }
}

我的状态文件;


enum Status { INITIAL, LOADING, COMPLETED, ERROR }

class LoginState extends Equatable {
final Status loginStatus;
final String? error;
final bool isAuthenticated;

LoginState(
   {required this.loginStatus, this.error, required this.isAuthenticated});

factory LoginState.initial() {
 return LoginState(loginStatus: Status.INITIAL, isAuthenticated: false);
}

LoginState copyWith(
   {required Status loginStatus,
   String? error,
   required bool isAuthenticated}) {
 return LoginState(
     loginStatus: loginStatus,
     error: error,
     isAuthenticated: isAuthenticated);
}

@override
List<Object?> get props => [loginStatus, error, isAuthenticated];
}

然后是我的听众

return BlocListener<LoginBloc, LoginState>(
      listener: (context, state) {
        if (state.loginStatus == Status.COMPLETED) {
          Navigator.of(context).pushReplacementNamed('/dashboard');
        }
        if (state.loginStatus == Status.ERROR) {
          final snackBar = SnackBar(
            backgroundColor: Colors.black,
            content: Text(state.error!),
          );
          ScaffoldMessenger.of(context).showSnackBar(snackBar);
        }
        print(state);
      },

我知道监听器只会在每次状态更改时调用一次,但就好像监听器没有注册任何状态更改一样。我们将不胜感激!

【问题讨论】:

  • 你应该改变一些东西来重建状态。例如,您需要在某处调用 login 方法来更改状态。在您的代码中,您始终处于初始状态。每次你需要刷新你的状态,你应该调用emit(NewState)
  • 是的,我只是没有上传那部分。我有一个调用登录方法的按钮。我的集团建设者工作正常。 bloc 监听器的行为不像我想要的那样
  • 您能否提供完整的代码,说明您如何将LoginBloc 提供给小部件树以及您侦听代码更改的完整小部件?

标签: flutter bloc flutter-bloc


【解决方案1】:

好的,我想我知道错误来自哪里。我有一个 blocbuilder,它根据当前状态显示不同的页面,并且在这些页面中包括具有 bloc 侦听器的登录页面。所以我删除了 bloc 构建器,只返回了登录页面,其中包含 bloc 侦听器,快餐栏被调用,因为它应该是。我使用 blocconsumer 来实现我想要实现的目标。

class LoginScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return BlocConsumer<LoginBloc, LoginState>(
      listener: (context, state) {
        if (state is LoginError) {
          final snackBar = SnackBar(
            backgroundColor: Colors.black,
            content: Text(state.error),
          );
          ScaffoldMessenger.of(context).showSnackBar(snackBar);
        }
      },
      builder: (context, state) {
        if (state is LoginLoading) {
          return ProgressIndication();
        } else if (state is LoginSuccess) {
          return DashboardScreen();
        }
        return Login();
      },
    );
  }
}

【讨论】:

    猜你喜欢
    • 2019-07-04
    • 1970-01-01
    • 1970-01-01
    • 2020-02-02
    • 2022-07-07
    • 2017-01-25
    • 1970-01-01
    • 2016-09-20
    • 1970-01-01
    相关资源
    最近更新 更多