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
+8 -2
View File
@@ -16,6 +16,13 @@ class NavCubit extends Cubit<NavState> {
NavCubit() : super(NavInitial());
static NavCubit of(context) => BlocProvider.of<NavCubit>(context);
//functoin to do the important work when the user logs out
static clear() {
pageIndex = 0;
pageController.jumpToPage(0);
}
static int pageIndex = 0;
static Map<String, List<Widget>> appBarActions = {
@@ -139,11 +146,10 @@ class NavCubit extends Cubit<NavState> {
const MenuView(),
];
final PageController pageController = PageController();
static final PageController pageController = PageController();
void updatePageIndex(int index) {
pageIndex = index;
print('index: $index');
pageController.animateToPage(index,
duration: const Duration(milliseconds: 150), curve: Curves.easeIn);
emit(NavChangePage());
+21 -5
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));
}
}
}
@@ -18,6 +18,18 @@ class SpacesError extends SpacesState {
SpacesError(this.errMessage);
}
class SpaceRoomsLoading extends SpacesLoading {}
class SpaceRoomsLoaded extends SpacesLoading {
final List<RoomModel> rooms;
SpaceRoomsLoaded(this.rooms);
}
class SpaceRoomsError extends SpacesError {
SpaceRoomsError(super.errMessage);
}
class SpacesSelected extends SpacesState {
final SpaceModel space;