Bloc: After catching error, flutter_bloc stop working

Created on 17 Sep 2019  路  2Comments  路  Source: felangel/bloc

Describe the bug
no detection on transition after catching error

Expected behavior
expecting user able to login even after catching error

Screenshots
flutter_bloc_bug

Additional context
This was initially a flutter_web project, currently migrated to flutter after announcement of Flutter 1.9.

auth bloc

import 'dart:async';
import 'package:bloc/bloc.dart';
import 'package:jaga_crew/models/login_data.dart';
import 'package:jaga_crew/resources/repositories/user_repo.dart';
import 'package:firebase/firebase.dart' as firebase;
import './bloc.dart';

class AuthBloc extends Bloc<AuthEvent, AuthState> {
  final UserRepo userRepo;
  AuthBloc({this.userRepo});

  @override
  AuthState get initialState => Initialize();

  @override
  Stream<AuthState> mapEventToState(AuthEvent event) async* {
    if (event is AutoAuthenticate) {
      yield* _autoAuthenticate(event.user);
    } else if (event is Authenticate) {
      yield* _authenticate(event.loginData);
    } else if (event is Unauthenticate) {
      yield* _unauthenticate();
    }
  }

  Stream<AuthState> _autoAuthenticate(firebase.User user) async* {
    yield Authenticated(user);
  }

  Stream<AuthState> _authenticate(LoginData loginData) async* {
    yield Authenticating();
    try {
      final user = await userRepo.loginWithEmailAndPassword(
          loginData.email, loginData.password);
      yield Authenticated(user);
    } catch (e) {
      yield Unauthenticated(errorMsg: e.toString());
    }
  }

  Stream<AuthState> _unauthenticate() async* {
    await userRepo.logout();
    yield Unauthenticated();
  }
}

auth state

import 'package:equatable/equatable.dart';
import 'package:meta/meta.dart';
import 'package:firebase/firebase.dart' as firebase;

@immutable
abstract class AuthState extends Equatable {
  AuthState([List props = const <dynamic>[]]) : super(props);
}

class Initialize extends AuthState {}

class Authenticating extends AuthState {}

class Authenticated extends AuthState {
  final firebase.User user;

  Authenticated(this.user) : super([user]);
}

class Unauthenticated extends AuthState {
  final String errorMsg;

  Unauthenticated({this.errorMsg}) : super([errorMsg]);
}

auth event

import 'package:equatable/equatable.dart';
import 'package:jaga_crew/models/login_data.dart';
import 'package:firebase/firebase.dart' as firebase;
import 'package:meta/meta.dart';

@immutable
abstract class AuthEvent extends Equatable {
  AuthEvent([List props = const <dynamic>[]]) : super(props);
}

class AutoAuthenticate extends AuthEvent {
  final firebase.User user;
  AutoAuthenticate(this.user) : super([user]);
}

class Authenticate extends AuthEvent {
  final LoginData loginData;

  Authenticate(this.loginData) : super([loginData]);
}

class Unauthenticate extends AuthEvent {}

on login button pressed

onPressed: state is Authenticating
        ? null
        : () {
             activateAutoValidate();
             if (_formKey.currentState.validate()) {
               _formKey.currentState.save();
               authBloc.dispatch(Authenticate(_loginData));
             }
           },
duplicate

Most helpful comment

Thanks a lot @felangel ! That helps!
I would like to take this opportunity to thank you and your team (if any) for creating this superb library which make bloc a lot easier to execute and understand! Keep the good work!

All 2 comments

Hi @pczn0327 馃憢
Thanks for opening an issue!

I believe this is a duplicate of https://github.com/felangel/bloc/issues/115. Closing for now but feel free to comment with additional information if running in release mode doesn鈥檛 resolve the issue 馃憤

Thanks a lot @felangel ! That helps!
I would like to take this opportunity to thank you and your team (if any) for creating this superb library which make bloc a lot easier to execute and understand! Keep the good work!

Was this page helpful?
0 / 5 - 0 ratings