Flutter 颤振阵营同时增加2个事件
我想检查我的应用程序中的用户internet连接和firebase身份验证状态更改。我正在使用Flatter bloc进行我的应用程序的状态管理。但是当在一个initstate中调用不同的2.add(event)时,第一个总是运行并更改状态,而第二个没有运行并没有更改状态。我怎么了 我的集团:Flutter 颤振阵营同时增加2个事件,flutter,dart,bloc,flutter-bloc,Flutter,Dart,Bloc,Flutter Bloc,我想检查我的应用程序中的用户internet连接和firebase身份验证状态更改。我正在使用Flatter bloc进行我的应用程序的状态管理。但是当在一个initstate中调用不同的2.add(event)时,第一个总是运行并更改状态,而第二个没有运行并没有更改状态。我怎么了 我的集团: class ControllerBloc extends Bloc<ControllerEvent, ControllerState> { ControllerBloc() : super
class ControllerBloc extends Bloc<ControllerEvent, ControllerState> {
ControllerBloc() : super(ControllerInitial());
AuthApiClient _authApiClient = getIt<AuthApiClient>();
@override
Stream<ControllerState> mapEventToState(
ControllerEvent event,
) async* {
if (event is ControllInternetConnection) {
yield* internetControll();
}
if (event is ControllUserAuth) {
debugPrint("wwwwgeldi");
yield* userAuthControl();
}
// TODO: implement mapEventToState
}
Stream<ControllerState> internetControll() async* {
Stream<DataConnectionStatus> connectionState =
DataConnectionChecker().onStatusChange;
await for (DataConnectionStatus status in connectionState) {
switch (status) {
case DataConnectionStatus.connected:
debugPrint("Bağlandı");
yield InternetConnectedState();
break;
case DataConnectionStatus.disconnected:
debugPrint("Kesildi");
yield InternetConnectionLostState();
break;
}
}
}
Stream<ControllerState> userAuthControl() async* {
FirebaseAuth firebaseAuth = _authApiClient.authInstanceAl();
debugPrint("geldi");
Stream<User> authStream = firebaseAuth.authStateChanges();
_authApiClient.authInstanceAl().signOut();
await for (User authUserResult in authStream) {
if (authUserResult == null) {
yield UserAuthControlError();
}
}
}
}
class ControllerBloc扩展了Bloc{
ControllerBloc():超级(ControllerInitial());
AuthApiClient_AuthApiClient=getIt();
@凌驾
流映射事件状态(
ControllerEvent事件,
)异步*{
if(事件为ControllInternetConnection){
收益率*internetControll();
}
if(事件为ControlUserAuth){
调试打印(“WWGELDI”);
yield*userAuthControl();
}
//TODO:实现mapEventToState
}
流internetControll()异步*{
流连接状态=
DataConnectionChecker().onStatusChange;
等待(连接状态中的DataConnectionStatus状态){
开关(状态){
案例DataConnectionStatus.connected:
debugPrint(“巴兰”);
产生InternetConnectedState();
打破
案例DataConnectionStatus.disconnected:
调试打印(“Kesildi”);
产生InternetConnectionLostState();
打破
}
}
}
流userAuthControl()异步*{
FirebaseAuth FirebaseAuth=_authApiClient.authInstanceAl();
调试打印(“geldi”);
Stream authStream=firebaseAuth.authStateChanges();
_authApiClient.authInstanceAl().signOut();
等待(authStream中的用户authUserResult){
if(authUserResult==null){
产生UserAuthControlError();
}
}
}
}
调用我的事件的我的页面
class _NavigationPageState extends State<NavigationPage> {
ControllerBloc controllerBloc;
@override
void initState() {
controllerBloc= BlocProvider.of<ControllerBloc>(context);
controllerBloc.add(ControllInternetConnection());
controllerBloc.add(ControllUserAuth());
super.initState();
}
class\u导航页面状态扩展状态{
ControllerBloc ControllerBloc;
@凌驾
void initState(){
controllerBloc=BlocProvider.of(上下文);
添加(ControlInternetConnection());
controllerBloc.add(ControllUserAuth());
super.initState();
}
如果我正确理解这一点,那么在我看来,您正试图用一个组解决两个不同的问题。我看不出为什么internet连接和用户身份验证必须在一个组中,而我只是将这两个组分开
正如中的讨论,使用集团的目的是围绕可预测性的想法。您可以覆盖现有的集团事件流,但我个人认为这对于您尝试做的事情来说太复杂了
因此,我建议,或者创建两个单独的组,或者将整个过程合并到一个事件中,在对用户进行身份验证之前检查internet连接,然后根据错误返回不同的状态