Resolved error for goup page
This commit is contained in:
@@ -3,6 +3,7 @@ import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'group_event.dart';
|
||||
import 'group_state.dart';
|
||||
import '../../repositories/group_repository.dart';
|
||||
import '../../data/models/group.dart';
|
||||
|
||||
class GroupBloc extends Bloc<GroupEvent, GroupState> {
|
||||
final GroupRepository _repository;
|
||||
@@ -10,6 +11,7 @@ class GroupBloc extends Bloc<GroupEvent, GroupState> {
|
||||
|
||||
GroupBloc(this._repository) : super(GroupInitial()) {
|
||||
on<LoadGroupsByUserId>(_onLoadGroupsByUserId);
|
||||
on<_GroupsUpdated>(_onGroupsUpdated); // NOUVEAU événement interne
|
||||
on<LoadGroupsByTrip>(_onLoadGroupsByTrip);
|
||||
on<CreateGroup>(_onCreateGroup);
|
||||
on<CreateGroupWithMembers>(_onCreateGroupWithMembers);
|
||||
@@ -19,22 +21,58 @@ class GroupBloc extends Bloc<GroupEvent, GroupState> {
|
||||
on<DeleteGroup>(_onDeleteGroup);
|
||||
}
|
||||
|
||||
// NOUVEAU : Charger les groupes par userId
|
||||
Future<void> _onLoadGroupsByUserId(
|
||||
LoadGroupsByUserId event,
|
||||
Emitter<GroupState> emit,
|
||||
) async {
|
||||
emit(GroupLoading());
|
||||
|
||||
await _groupsSubscription?.cancel();
|
||||
print('===== GroupBloc: _onLoadGroupsByUserId START =====');
|
||||
|
||||
_groupsSubscription = _repository.getGroupsByUserId(event.userId).listen(
|
||||
(groups) => emit(GroupsLoaded(groups)),
|
||||
onError: (error) => emit(GroupError(error.toString())),
|
||||
);
|
||||
try {
|
||||
emit(GroupLoading());
|
||||
print('>>> GroupBloc: État GroupLoading émis');
|
||||
|
||||
await _groupsSubscription?.cancel();
|
||||
print('>>> GroupBloc: Ancien subscription annulé');
|
||||
|
||||
_groupsSubscription = _repository.getGroupsByUserId(event.userId).listen(
|
||||
(groups) {
|
||||
print('===== GroupBloc: Stream reçu ${groups.length} groupes =====');
|
||||
// Utiliser un événement interne au lieu d'émettre directement
|
||||
add(_GroupsUpdated(groups));
|
||||
},
|
||||
onError: (error) {
|
||||
print('===== GroupBloc: Erreur stream: $error =====');
|
||||
add(_GroupsUpdated([], error: error.toString()));
|
||||
},
|
||||
);
|
||||
|
||||
print('>>> GroupBloc: Subscription créé avec succès');
|
||||
} catch (e, stackTrace) {
|
||||
print('===== GroupBloc: Exception _onLoadGroupsByUserId =====');
|
||||
print('Exception: $e');
|
||||
print('StackTrace: $stackTrace');
|
||||
emit(GroupError(e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
// NOUVEAU: Handler pour les mises à jour du stream
|
||||
Future<void> _onGroupsUpdated(
|
||||
_GroupsUpdated event,
|
||||
Emitter<GroupState> emit,
|
||||
) async {
|
||||
print('===== GroupBloc: _onGroupsUpdated =====');
|
||||
print('Groupes reçus: ${event.groups.length}');
|
||||
|
||||
if (event.error != null) {
|
||||
print('>>> Émission GroupError: ${event.error}');
|
||||
emit(GroupError(event.error!));
|
||||
} else {
|
||||
print('>>> Émission GroupsLoaded avec ${event.groups.length} groupes');
|
||||
emit(GroupsLoaded(event.groups));
|
||||
print('>>> GroupsLoaded émis avec succès !');
|
||||
}
|
||||
}
|
||||
|
||||
// Charger les groupes d'un voyage (conservé)
|
||||
Future<void> _onLoadGroupsByTrip(
|
||||
LoadGroupsByTrip event,
|
||||
Emitter<GroupState> emit,
|
||||
@@ -52,7 +90,6 @@ class GroupBloc extends Bloc<GroupEvent, GroupState> {
|
||||
}
|
||||
}
|
||||
|
||||
// Créer un groupe simple
|
||||
Future<void> _onCreateGroup(
|
||||
CreateGroup event,
|
||||
Emitter<GroupState> emit,
|
||||
@@ -69,7 +106,6 @@ class GroupBloc extends Bloc<GroupEvent, GroupState> {
|
||||
}
|
||||
}
|
||||
|
||||
// Créer un groupe avec ses membres
|
||||
Future<void> _onCreateGroupWithMembers(
|
||||
CreateGroupWithMembers event,
|
||||
Emitter<GroupState> emit,
|
||||
@@ -86,7 +122,6 @@ class GroupBloc extends Bloc<GroupEvent, GroupState> {
|
||||
}
|
||||
}
|
||||
|
||||
// Ajouter un membre
|
||||
Future<void> _onAddMemberToGroup(
|
||||
AddMemberToGroup event,
|
||||
Emitter<GroupState> emit,
|
||||
@@ -99,7 +134,6 @@ class GroupBloc extends Bloc<GroupEvent, GroupState> {
|
||||
}
|
||||
}
|
||||
|
||||
// Supprimer un membre
|
||||
Future<void> _onRemoveMemberFromGroup(
|
||||
RemoveMemberFromGroup event,
|
||||
Emitter<GroupState> emit,
|
||||
@@ -112,7 +146,6 @@ class GroupBloc extends Bloc<GroupEvent, GroupState> {
|
||||
}
|
||||
}
|
||||
|
||||
// Mettre à jour un groupe
|
||||
Future<void> _onUpdateGroup(
|
||||
UpdateGroup event,
|
||||
Emitter<GroupState> emit,
|
||||
@@ -125,7 +158,6 @@ class GroupBloc extends Bloc<GroupEvent, GroupState> {
|
||||
}
|
||||
}
|
||||
|
||||
// Supprimer un groupe
|
||||
Future<void> _onDeleteGroup(
|
||||
DeleteGroup event,
|
||||
Emitter<GroupState> emit,
|
||||
@@ -140,7 +172,19 @@ class GroupBloc extends Bloc<GroupEvent, GroupState> {
|
||||
|
||||
@override
|
||||
Future<void> close() {
|
||||
print('===== GroupBloc: close() =====');
|
||||
_groupsSubscription?.cancel();
|
||||
return super.close();
|
||||
}
|
||||
}
|
||||
|
||||
// NOUVEAU: Événement interne pour les mises à jour du stream
|
||||
class _GroupsUpdated extends GroupEvent {
|
||||
final List<Group> groups;
|
||||
final String? error;
|
||||
|
||||
const _GroupsUpdated(this.groups, {this.error});
|
||||
|
||||
@override
|
||||
List<Object?> get props => [groups, error];
|
||||
}
|
||||
Reference in New Issue
Block a user