1

I am working with flutter_bloc and trying to understand how it works entirely.

I have Profile Screen. Where in the User should enter his details if previously not existed else should update the details.

Logic: If the user already exists then i fill up the textfields prior to loading , else the textfields are left blank

Problem: I have worked out of achieving the above mentioned goal , but everything seems to work only the first time , Once i save the profile and come back to the profile page it doesn't load the data and textfields are empty even if the user exists.

User_Cubit

class UserCubit extends Cubit<UserState> {
  UserCubit() : super(UserInitialState()) {
    checkIfUserExists();
  }
  void checkIfUserExists() {
    emit(UserLoadingState());
    ...
      if (userExists) {
        emit(UserExists(user));
      } else {
        emit(UserNotExists());
      }
    });
  }

Profile Screen

class _MyProfileScreenState extends State<MyProfileScreen> {
  TextEditingController? fullNameController;
  TextEditingController? mailAddressController;
  late UserCubit _userCubit;

  @override
  void initState() {
    fullNameController = TextEditingController();
    mailAddressController = TextEditingController();
    _userCubit = UserCubit(); // initializing the cubit here
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: MultiBlocListener(
          listeners: [
            BlocListener<UserCubit, UserState>(
              listener: (context, state) {
                if (state is UserExists) {
                  appUser = state.user;
                  mailAddressController!.text = appUser!.email; // Loading the fields
                  fullNameController!.text = appUser!.fullName; // Loading the fields
                }
              },
            )
          ],
          child: BlocBuilder<UserCubit, UserState>(
              builder: (context, state) {
            if (state is UserLoadingState) {
              return const Center(child: CircularProgressIndicator());
            }
            return Container(
                  TextFormField() // For fullName
                  TextFormField() // For EmailAddress
            )
       )
    );
}

Why does this functionality work only the first time not the consecutive times. Thougth the UserCubit is intialized in initstate ?

Any further suggestions to improve this logic by not initializing the UserCubit on every page render would be also appreciated !!

krishnaacharyaa
  • 14,953
  • 4
  • 49
  • 88
  • are you using Equatable in your states? – Almis Dec 11 '22 at 15:55
  • As i am using Cubit , I am not using equatable – krishnaacharyaa Dec 11 '22 at 16:17
  • 1
    You can use Equatable with cubit but I guess that is not your issue. Also why did you initialise your cubit like that and not with a provider? https://pub.dev/packages/flutter_bloc#maindart – Almis Dec 11 '22 at 16:43
  • I have done initialisation in the main dart using bloc provider – krishnaacharyaa Dec 11 '22 at 18:44
  • What you need to do is to move the bloc provider to your profile page. You can also remove that initialise part, it's obsolete. What happens is that you leave the page the state is still the same, so when you emit nothing happens because it's already in that state. – Almis Dec 11 '22 at 19:17

0 Answers0