为什么我的flutter应用程序异常总是无法捕获

e37o9pze  于 2023-01-18  发布在  Flutter
关注(0)|答案(1)|浏览(148)

我正在使用我正在使用flutter和改型与riverpod作为一个状态管理,但每当我试图运行我的代码,它得到uncaught和指向生成的文件,每当我使用一个未来的提供者.代码如下

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');
  });
});

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

但是如果我使用状态提供程序并捕获错误,它就会被捕获。有人有主意吗

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:该错误是401未授权错误,应该被捕获。

n9vozmp4

n9vozmp41#

也许你可以尝试从msvcode边栏的调试选项卡中取消选中

相关问题