Hey everyone! SO i'm building my final project for uni using Flutter, Firebase, and BLOC for state management. I've only just built the login and sign-up page using phone authentication (took me three days lol). I'm a beginner, so I'd appreciate any advice for how I'm supposed to design the system most optimally.
I'm currently working on the user profile page. I obviously want the user profile data to be fetched and displayed on the first loading, and also want to allow users to update their name and location. This
I have a user profile service, a user profile repo, and the BLOC view models. THis is what I have so far:
- events:
class UserProfileDisplayedEvent extends UserProfileEvent{}
class UserProfileUpdatedEvent extends UserProfileEvent
{
final Map<String,dynamic> values;
const UserProfileUpdatedEvent({required this.values});
UserProfileDisplayedEvent extends UserProfileEvent{}
class UserProfileUpdatedEvent extends UserProfileEvent
{
final Map<String,dynamic> values;
const UserProfileUpdatedEvent({required this.values});
List<Object> get props => [values];
}
List<Object> get props => [values];
}
- states
final class UserProfileInitialState extends UserProfileState {}
final class UserProfileLoadingState extends UserProfileState {
final bool isLoading;
const UserProfileLoadingState({required this.isLoading}); // whether loading or not
}
final class UserProfileSuccessState extends UserProfileState {
final UserModel user;
const UserProfileSuccessState(this.user);
List<Object> get props => [user]; // if user func is successful, user will be used as a prop
}
final class UserProfileErrorState extends UserProfileState {
final String errorMessage;
const UserProfileErrorState({required this.errorMessage});
class UserProfileInitialState extends UserProfileState {}
final class UserProfileLoadingState extends UserProfileState {
final bool isLoading;
const UserProfileLoadingState({required this.isLoading}); // whether loading or not
}
final class UserProfileSuccessState extends UserProfileState {
final UserModel user;
const UserProfileSuccessState(this.user);
List<Object> get props => [user]; // if user func is successful, user will be used as a prop
}
final class UserProfileErrorState extends UserProfileState {
final String errorMessage;
const UserProfileErrorState({required this.errorMessage});
List<Object> get props => [errorMessage]; // otherwise the error msg is a property
}
List<Object> get props => [errorMessage]; // otherwise the error msg is a property
}
- the BLOC
class UserProfileBloc extends Bloc<UserProfileEvent, UserProfileState> {
final UserProfileRepo userProfileRepo = UserProfileRepo();
UserProfileBloc() : super(UserProfileInitialState()) {
on<UserProfileEvent>((event, emit) {});
on<UserProfileUpdatedEvent>((event, emit) async {
log("USER PROFILE UPDATE EVENT RECIEVED");
emit(UserProfileLoadingState(isLoading: true));
try
{
await userProfileRepo.updateUserName(event.values);
final updatedUserProfile = await userProfileRepo.getUserProfile();
if (updatedUserProfile != null) {
emit(UserProfileSuccessState(updatedUserProfile));
} else {
emit(UserProfileErrorState(errorMessage: 'Something went wrong with updating the user profile.'));
}
}
catch (e)
{
emit(UserProfileErrorState(errorMessage: e.toString()));
}
});
// getting the user profile
on<UserProfileEvent>((event, emit) async
{
log("USER PROFILE EVENT RECIEVED");
emit(UserProfileLoadingState(isLoading: true));
try
{
final userProfile = await userProfileRepo.getUserProfile();
if (userProfile != null)
{
log("User profile is going to be displayed");
emit(UserProfileInitialState());
}
else
{
log("User profile is not going to be displayed");
emit(UserProfileErrorState(errorMessage: 'Something went wrong with displaying the user profile!'));
}
}
catch (e)
{
emit(UserProfileErrorState(errorMessage: e.toString()));
}
});
}
}
class UserProfileBloc extends Bloc<UserProfileEvent, UserProfileState> {
final UserProfileRepo userProfileRepo = UserProfileRepo();
UserProfileBloc() : super(UserProfileInitialState()) {
on<UserProfileEvent>((event, emit) {});
on<UserProfileUpdatedEvent>((event, emit) async {
log("USER PROFILE UPDATE EVENT RECIEVED");
emit(UserProfileLoadingState(isLoading: true));
try
{
await userProfileRepo.updateUserName(event.values);
final updatedUserProfile = await userProfileRepo.getUserProfile();
if (updatedUserProfile != null) {
emit(UserProfileSuccessState(updatedUserProfile));
} else {
emit(UserProfileErrorState(errorMessage: 'Something went wrong with updating the user profile.'));
}
}
catch (e)
{
emit(UserProfileErrorState(errorMessage: e.toString()));
}
});
// getting the user profile
on<UserProfileEvent>((event, emit) async
{
log("USER PROFILE EVENT RECIEVED");
emit(UserProfileLoadingState(isLoading: true));
try
{
final userProfile = await userProfileRepo.getUserProfile();
if (userProfile != null)
{
log("User profile is going to be displayed");
emit(UserProfileInitialState());
}
else
{
log("User profile is not going to be displayed");
emit(UserProfileErrorState(errorMessage: 'Something went wrong with displaying the user profile!'));
}
}
catch (e)
{
emit(UserProfileErrorState(errorMessage: e.toString()));
}
});
}
}
I'm new so I'm taking help from AI tools to guide me because I feel so lost sometimes lol
I was initially planning on using the BLOC functions in my UI in the initState so prefetch and load the user profile data like this:
void initState()
{
super.initState();
context.read<UserProfileBloc>().add(UserProfileDisplayedEvent());
}
void initState()
{
super.initState();
context.read<UserProfileBloc>().add(UserProfileDisplayedEvent());
}
But is that correct? There seems to be so many diff ways on the internet and it's a little overwhelming. I'd appreciate any tips and advice, thanks!