Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

After catching error, flutter_bloc stop working #521

Closed
PhilipChng opened this issue Sep 17, 2019 · 2 comments
Closed

After catching error, flutter_bloc stop working #521

PhilipChng opened this issue Sep 17, 2019 · 2 comments
Assignees
Labels
duplicate This issue or pull request already exists

Comments

@PhilipChng
Copy link

PhilipChng commented Sep 17, 2019

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));
             }
           },
@felangel felangel self-assigned this Sep 17, 2019
@felangel
Copy link
Owner

Hi @pczn0327 👋
Thanks for opening an issue!

I believe this is a duplicate of #115. Closing for now but feel free to comment with additional information if running in release mode doesn’t resolve the issue 👍

@felangel felangel added the duplicate This issue or pull request already exists label Sep 17, 2019
@PhilipChng
Copy link
Author

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

2 participants