Outline:
- Navigate to profile page
- Pass in user id to fetch doc from firestore
- Pass the retrieved data to state.copyWith(data:data)
- yield a successful status and present ui
My problem is that when i am using state.copyWith(data:data) the state is not being updated even though the data 100% exists as I can print it in console.
Code:
UI:
class ProfileView extends StatelessWidget {
final String uid;
final UserRepository userRepository = UserRepository();
ProfileView({required this.uid});
@override
Widget build(BuildContext context) {
// Init repository
return BlocProvider<ProfileBloc>(
create: (context) => ProfileBloc(
// Should be able to pull user repo from context
userRepository: UserRepository(),
isCurrentUser: UserRepository().isCurrentUser(uid))
..add(InitializeProfile(uid: uid)),
// Get User Doc
child: _profileView(context));
}
Widget _profileView(context) {
return BlocListener<ProfileBloc, ProfileState>(
listener: (context, state) {
if (state.imageSourceActionSheetIsVisible) {
_showImageSourceActionSheet(context);
}
final loadingStatus = state.loadingStatus;
if (loadingStatus is LoadingFailed) {
showSnackBar(context, state.loadingStatus.exception.toString());
}
if (loadingStatus is LoadingInProgress) {
LoadingView();
}
if (loadingStatus is LoadingFailed) {
LoadingFailedView(exception: loadingStatus.exception.toString());
}
},
child: Scaffold(
appBar: _appBar(),
body: _profilePage(),
bottomNavigationBar: bottomNavbar(context),
),
);
}
BLoC:
class ProfileBloc extends Bloc<ProfileEvent, ProfileState> {
final bool isCurrentUser;
final UserRepository userRepository;
final _imagePicker = ImagePicker();
ProfileBloc({
required this.isCurrentUser,
required this.userRepository,
}) : super(ProfileState(
isCurrentUser: isCurrentUser, loadingStatus: LoadingInProgress()));
@override
Stream<ProfileState> mapEventToState(
ProfileEvent event,
) async* {
if (event is InitializeProfile) {
yield* _initProfile(uid: event.uid);
}
}
Stream<ProfileState> _initProfile({required String uid}) async* {
// Loading View
yield state.copyWith(loadingStatus: LoadingInProgress());
// Fetch profile data
try {
final snapshot = await userRepository.getUserDoc(uid);
if (snapshot.exists) {
final data = snapshot.data();
print(data['fullName']);
yield state.copyWith(
fullName: data["fullName"].toString(),
);
print(state.fullName);
yield state.copyWith(loadingStatus: LoadingSuccess());
}
else {
yield state.copyWith(
loadingStatus: LoadingFailed("Profile Data Not present :("));
}
} catch (e) {
print(e);
}
}
State:
part of 'profile_bloc.dart';
class ProfileState {
final bool isCurrentUser;
final String? fullName;
final LoadingStatus loadingStatus;
bool imageSourceActionSheetIsVisible;
ProfileState({
required bool isCurrentUser,
this.fullName,
this.loadingStatus = const InitialLoadingStatus(),
imageSourceActionSheetIsVisible = false,
}) : this.isCurrentUser = isCurrentUser,
this.imageSourceActionSheetIsVisible = imageSourceActionSheetIsVisible;
ProfileState copyWith({
bool? isCurrentUser,
String? fullName,
LoadingStatus? loadingStatus,
bool? imageSourceActionSheetIsVisible,
}) {
print('State $fullName');
print('State ${this.fullName}');
return ProfileState(
isCurrentUser: this.isCurrentUser,
fullName: fullName ?? this.fullName,
loadingStatus: loadingStatus ?? this.loadingStatus,
imageSourceActionSheetIsVisible: imageSourceActionSheetIsVisible ??
this.imageSourceActionSheetIsVisible,
);
}
}
Newly updated Code implementing Bloc builder as opposed to listener as suggested
Widget _profileView(context) {
return BlocBuilder<ProfileBloc, ProfileState>(builder: (context, state) {
final loadingStatus = state.loadingStatus;
if (loadingStatus is LoadingInProgress) {
return LoadingView();
}
if (loadingStatus is LoadingFailed) {
return LoadingFailedView(exception: loadingStatus.exception.toString());
}
if (loadingStatus is LoadingSuccess) {
return Scaffold(
appBar: _appBar(),
body: _profilePage(),
bottomNavigationBar: bottomNavbar(context),
);
}
return LoadingView();
});
}
This code remains stuck on loading screen, when i print out the various states it is registered as an event however the state prints out null after using copy with
Thanks for the help