1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Flutter] Bloc changes state when the response is different but the status Code is 200. (...

Discussão em 'Mobile' iniciado por Stack, Outubro 4, 2024 às 05:12.

  1. Stack

    Stack Membro Participativo

    I have bloc which I used to login, The problem is if the phone I'm entering is related to a deleted account the bloc changes state to ErrorState.

    if the phone is correct the json looks like this

    {
    "status": true,
    "msg": "Done",
    "data": {
    "name": "",
    "phone": "",
    "email": "",
    "status": "",
    "address": "",
    "address_short": "",
    "address_link": "",
    "api_token": "",
    "otp": 2379
    }}


    If the account is deleted this is the json response

    {
    "status": false,
    "msg": "deleted account"
    }


    bloc snippet

    AuthBloc({required this.auth, required this.sendOTP}) : super(AuthInitial()) {
    on<AuthEvent>((event, emit) async {
    if (event is LoginOrRegisterEvent) {
    emit(LoadingState());

    final loginOrRegister = await auth.call(
    phone: event.phone,
    name: event.phone,
    );
    loginOrRegister.fold(
    (failure) {
    emit(ErrorAuthState(message: AppFunctions.mapFailureToMessage(failure: failure)));
    },
    (auth) {
    emit(LoadedAuthState(authEntity: auth));
    },
    );
    }
    }); }


    I'm using clean architecture here is my remote data source code

    class AuthRemoteDataSourceImpl implements AuthRemoteDataSource {
    @override
    Future<AuthModel> auth({
    required String phone,
    required String name,
    String? fcmToken,
    }) async {
    final response = await DioHelper.postData(
    url: ApiConstants.login,
    data: {
    'phone': phone,
    'name': name,
    'fcmToken': fcmToken ?? '',
    },
    headers: {},
    );

    if (response!.statusCode == 200) {
    final authModel = AuthModel.fromJson(response.data);
    if (kDebugMode) {
    print('--------------- user Token --------------------');
    print(authModel.token);
    print('------------------------------------------------');
    }

    return authModel;
    } else {
    throw ServerException();
    }
    }}


    here is my Auth Repository

    class AuthRepositoryImpl implements AuthRepository {
    final AuthRemoteDataSource authRemoteDataSource;
    final NetworkInfo networkInfo;

    AuthRepositoryImpl({required this.authRemoteDataSource, required this.networkInfo});

    @override
    Future<Either<Failure, AuthEntity>> auth({
    required String phone,
    required String name,
    String? fcmToken,
    }) async {
    if (await networkInfo.isConnected) {
    try {
    final remoteProfile = await authRemoteDataSource.auth(
    phone: phone,
    name: name,
    fcmToken: fcmToken ?? '',
    );
    return Right(remoteProfile);
    } on ServerException {
    return Left(ServerFailure());
    } catch (error) {
    return Left(ServerFailure());
    }
    } else {
    return Left(NoInternetConnectionFailure());
    }
    }
    }

    Continue reading...

Compartilhe esta Página