mirror of
https://github.com/SyncrowIOT/syncrow-app.git
synced 2025-11-27 20:14:54 +00:00
getting spaces and rooms from api {null checks}
This commit is contained in:
@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user