我正在尝试解决与 Flutter Bloc 相关的问题。我正在编辑其他人的代码以使其与最新的 flutter_bloc 版本一起使用,但我无法这样做。有人可以重写我的代码以便我可以运行它吗?我看到了很多答案,但我无法理解如何修复我自己的代码。
这是 all_categories_bloc.dart 的完整代码
class AllCategoriesBloc extends Bloc<AllCategoriesEvent, AllCategoriesState> {
AllCategoriesBloc({
this.apiRepository,
}) : super(AllCategoriesInitial()) {
on<GetAllCategories>(_onGetAllCategories);
}
final ApiRepository apiRepository;
Future<void> _onGetAllCategories(
GetAllCategories event,
Emitter<AllCategoriesState> emit,
) async {
try {
emit(const AllCategoriesLoading());
final categoriesModel = await apiRepository.fetchCategoriesList();
emit(AllCategoriesLoaded(categoriesModel));
if (categoriesModel.error != null) {
emit(AllCategoriesError(categoriesModel.error));
}
} catch (e) {
emit(
const AllCategoriesError(
"Failed to fetch all categories data. Is your device online ?",
),
);
}
}
}
Run Code Online (Sandbox Code Playgroud)
all_categories_event.dart 的代码
abstract class AllCategoriesEvent …Run Code Online (Sandbox Code Playgroud)