1

i am using am using flutter and retrofit with riverpod as a statemanagement but anytime i try to run my code it get uncaught and point to the generated file anytime i use a future provider. code below

final customCategoryFutureProvider =
    FutureProvider<List<SubCategory>>((ref) async {
  final repo = ref.watch(apiServicesProvider);
  return repo
      .getCustomSubCategories()
      .then((value) => value)
      .catchError((Object obj) {
    if (obj.runtimeType == DioError) {
      final res = (obj as DioError?)?.response;
      if (res?.statusCode == 401) {
        throw Exception('''
          Got error : ${res!.statusCode} ->
          ${res.statusMessage ?? ''} ->
          Please sign in''');
      }
      throw Exception('''
          Got error : ${res!.statusCode} ->
          ${res.statusMessage ?? ''}''');
    }
    throw Exception('Got error :Unknown Error $obj');
  });
});

or

final customCategoryFutureProvider =
    FutureProvider<List<SubCategory>>((ref) async {
  final repo = ref.watch(apiServicesProvider);
  return repo.getCustomSubCategories();
});

but if i use a state provider and catch the error it get caught. anyone have a idea

class CCNotifier extends StateNotifier<CustomCategoryState> {
  CCNotifier(this.api) : super(CustomCategoryState.initial()) {
    getCustom();
  }
  final ApiService api;

  Future<void> getCustom() async {
    try {
      state = state.copyWith(status: CCStatus.loading);
      final subCat = await api.getCustomSubCategories();
      state = state.copyWith(subCat: subCat, status: CCStatus.loaded);
    } on DioError catch (e) {
      if (e.response?.statusCode == 401) {
        state = state.copyWith(
          status: CCStatus.failure,
          failure: Exception('''
          Got error : ${e.response!.statusCode} -> 
          ${e.response!.statusMessage} -> 
          Please sign in'''),
        );
      }
      state = state.copyWith(
        status: CCStatus.failure,
        failure: Exception('''
          Got error : ${e.response!.statusCode} -> 
          ${e.response!.statusMessage}'''),
      );
    } on Exception catch (e) {
      state = state.copyWith(
        status: CCStatus.failure,
        failure: Exception('Got error :Unknown Error $e'),
      );
    }
  }
}

P.S: the error is a 401 unauthorized error which is supposed to be caught.

olu
  • 93
  • 7

1 Answers1

0

maybe you can try to uncheck this at debug tab from msvcode sidebar

enter image description here

CCP
  • 886
  • 1
  • 10
  • 30