【发布时间】:2021-08-06 23:16:53
【问题描述】:
当我点击他们网页上的后退按钮时,我的 StateNotifiers 中出现此错误。我已将其隔离在longRunningAPI 请求下方的位置。
Exception has occurred.
"Error: Bad state: Tried to use RunListNotifier after `dispose` was called.
我有这样的代码。
final runListController = StateNotifierProvider.autoDispose
.family<RunListNotifier, AsyncValue<List<Run>>, RunListParameter>(
(ref, param) {
return RunListNotifier(read: ref.read, param: param);
});
class RunListNotifier extends StateNotifier<AsyncValue<List<Run>>> {
RunListNotifier({required this.read, required this.param})
: super(AsyncLoading()) {
fetchViaAPI(param);
}
final Reader read;
final RunListParameter param;
void fetchViaAPI(RunListParameter param) async {
state = AsyncLoading();
try {
List<Run> stuff = await read(apiProvider).longRunningAPI(param: param);
state = AsyncData(stuff);
} catch (e) {
state = AsyncError(e);
}
}
}
在 catch 中简单地做这样的事情是否安全?
} catch (e) {
if (e.runtimeType.toString() == 'StateError') {
// ignore the error
} else {
state = AsyncError(e);
}
}
【问题讨论】: