Bloc: Form is not getting updated with initial values from shared preferences repository, when fetch event is dispatched.

Created on 3 Oct 2019  路  4Comments  路  Source: felangel/bloc

I hope it's ok to post my question here, not sure if you'd catch this on gitter with the amount of messages. I'm currently trying to implement an update user form where it loads initial values from a shared preferences repository class then you can update it after. Currently to test it out, I have a register/login page after that is successful, user data is saved into shared preferences via the custom shared preferences repository then it routes to the homepage with a bottom navigation bar with 4 tabs. I put a update user page in the first tab that provides the bloc to the form and dispatches the event to fetch values from the shared preferences repository. But the shared preferences are not being loaded into the form unless I switch to another tab and switch back. Not sure what it is, I suspect it might be cause my shared preferences repository class which is a singleton and has static class methods. But not quite sure, I used your firebase login and todos tutorial to guide me.

Here is a gist: https://gist.github.com/DanT29/0b2b3f29b06aff40f8c397d81972577a

question

Most helpful comment

@felangel
I solved the problem by putting await before my function call to save to SharedPreferences Repository in my register bloc! Such a silly mistake. Sorry for the hassle! I appreciate your amazing work with the flutter_bloc library!!

All 4 comments

Hi @DanT29 馃憢
Thanks for opening an issue!

I'll take a look shortly 馃憤

@DanT29 can you override onTransition and print the transitions in UpdateUserBloc and confirm whether or not a transition occurs after the first tab loads? Thanks!

@override
void onTransition(transition) {
  super.onTransition(transition);
  print(transition);
}

Hi @felangel

I overrided the onTransition on UpdateUserBloc below is my output in the debug console right after register button is pressed, I also added a print statement inside my _mapInitializeUpdateUserFormToState() function in UpdateUserBloc to print the firstName, lastname,handle and userId from shared preferences they came out null, except the userId (you can see below). Once again when I switched to another tab and back they printed out the values, I also added my register_bloc code to give more information on how I save to shared preferences :

I/flutter (30358): Transition { currentState: AuthenticationUnauthenticated, event: LoggedIn, nextState: AuthenticationLoading } I/flutter (30358): Transition { currentState: AuthenticationLoading, event: LoggedIn, nextState: AuthenticationAuthenticated } I/flutter (30358): UpdateUserFormUninitialized I/flutter (30358): From Update UserBloc: userId: 1180157777499131904, firstName:null, lastName: null, handle:null I/flutter (30358): Transition { currentState: UpdateUserState { I/flutter (30358): isFirstNameValid: true, I/flutter (30358): isLastNameValid: true, I/flutter (30358): isHandleValid: true, I/flutter (30358): isSubmitting: false, I/flutter (30358): isSuccess: false, I/flutter (30358): isFailure: false, I/flutter (30358): isLoaded: false, I/flutter (30358): firstName: , I/flutter (30358): lastName: , I/flutter (30358): handle: , I/flutter (30358): error: , I/flutter (30358): }, event: UpdateUserFormUninitialized, nextState: UpdateUserState { I/flutter (30358): isFirstNameValid: true, I/flutter (30358): isLastNameValid: true, I/flutter (30358): isHandleValid: true, I/flutter (30358): isSubmitting: false, I/flutter (30358): isSuccess: false, I/flutter (30358): isFailure: false, I/flutter (30358): isLoaded: true, I/flutter (30358): firstName: , I/flutter (30358): lastName: , I/flutter (30358): handle: , I/flutter (30358): error: , I/flutter (30358): } } I/flutter (30358): Transition { currentState: UpdateUserState { I/flutter (30358): isFirstNameValid: true, I/flutter (30358): isLastNameValid: true, I/flutter (30358): isHandleValid: true, I/flutter (30358): isSubmitting: false, I/flutter (30358): isSuccess: false, I/flutter (30358): isFailure: false, I/flutter (30358): isLoaded: false, I/flutter (30358): firstName: , I/flutter (30358): lastName: , I/flutter (30358): handle: , I/flutter (30358): error: ,

@felangel
I solved the problem by putting await before my function call to save to SharedPreferences Repository in my register bloc! Such a silly mistake. Sorry for the hassle! I appreciate your amazing work with the flutter_bloc library!!

Was this page helpful?
0 / 5 - 0 ratings