getting spaces and rooms from api {null checks}

This commit is contained in:
Mohammad Salameh
2024-03-13 13:52:22 +03:00
parent 0f3cc453ce
commit 024f15728b
26 changed files with 390 additions and 266 deletions

View File

@ -10,7 +10,11 @@ part 'spaces_state.dart';
class SpacesCubit extends Cubit<SpacesState> {
SpacesCubit() : super(SpacesInitial()) {
fetchSpaces();
fetchSpaces().then((value) {
if (selectedSpace != null) {
fetchRooms(selectedSpace!);
}
});
}
static SpacesCubit get(context) => BlocProvider.of(context);
@ -83,12 +87,24 @@ class SpacesCubit extends Cubit<SpacesState> {
emit(SpacesLoading());
try {
spaces = await SpacesAPI.getSpaces();
spaces.isNotEmpty ? selectSpace(spaces.first) : null;
emit(SpacesLoaded(spaces));
} on DioException catch (e) {
emit(SpacesError(e.message ?? "Something went wrong"));
throw ServerFailure.fromDioError(e);
} catch (e) {
emit(SpacesError(e.toString()));
emit(SpacesError(ServerFailure.fromDioError(e).errMessage));
}
}
fetchRooms(SpaceModel space) async {
emit(SpaceRoomsLoading());
try {
space.rooms = await SpacesAPI.getRooms(space.id!);
if (space.rooms != null) {
emit(SpaceRoomsLoaded(space.rooms!));
} else {
emit(SpaceRoomsError("No rooms found"));
}
} on DioException catch (e) {
emit(SpacesError(ServerFailure.fromDioError(e).errMessage));
}
}
}