Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: BlocBuilder doesn't receive state when emitted from async loop #4197

Open
Seferi opened this issue Jun 27, 2024 · 3 comments
Open

fix: BlocBuilder doesn't receive state when emitted from async loop #4197

Seferi opened this issue Jun 27, 2024 · 3 comments
Labels
bug Something isn't working question Further information is requested waiting for response Waiting for follow up

Comments

@Seferi
Copy link

Seferi commented Jun 27, 2024

Description
Hi there, I'm facing this issue where the state (RefreshedContactCount) is emitted but for some reason not being received by the BlocBuilder. All the other states are received and updating the UI just fine but RefreshedContactCount. Here is the method in the cubit which emits the state:


Future<void> refreshAllContacts() async {
    bool hasInternet = await InternetConnection().hasInternetAccess;
    if (!hasInternet) {
      emit(RefreshAllContactsFailure());
    } else {
      int refreshedContactsCount = 0;
      List<ProfileModel> contacts = getIt<UserCubit>().keyoxideUser.contacts;

      await Future.forEach(contacts, (contact) async {

        emit(RefreshingAllContacts());
        bool isContactRefreshed = await refreshContact(contact: contact, isRefreshingAllContacts: true);
        if (isContactRefreshed) {
          refreshedContactsCount++;

          emit(RefreshedContactCount(successfulRefreshCount: refreshedContactsCount));
        }
      });
      emit(RefreshAllContactsSuccess());
    }
  }

And this is the BlocBuilder which is suppose to receive the state:

BlocBuilder<ContactsCubit, ContactsState>(
  bloc: getIt<ContactsCubit>(),
  builder: (context, state) {
    if (state is RefreshingAllContacts) {

    }
    if (state is RefreshedContactCount) {
      refreshedContactCount = state.successfulRefreshCount;
    }
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 20.0),
      child: Center(
        child: Container(
            width: double.infinity,
            height: 200,
            decoration: BoxDecoration(
                color: Theme.of(context).brightness == Brightness.dark
                    ? Colors.black.withOpacity(0.9)
                    : Colors.white.withOpacity(0.9),
                borderRadius: const BorderRadius.all(Radius.circular(20))),
            child: Center(
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        const CircularProgressIndicator(),
                        const SizedBox(height: 10),
                        Text("contacts_refresh_all_count".localizeWithPlaceholders(
                              [refreshedContactCount.toString(), totalContactCount.toString()])!,
                          textAlign: TextAlign.center,
                        ),
                      ],
                    )
            )),
      ),
    );
  },
);

Expected Behavior
State gets received by the BlocBuilder or Consumer and updates the UI.

Additional Context
I have been using Bloc for years now and never faced this kind of issue, I'm guessing the loop might be causing the issue, causing some kind of race condition. I'd very much appreciate if someone could help or take a look. Thanks.

@Seferi Seferi added the bug Something isn't working label Jun 27, 2024
@FarisArmoush
Copy link

Where is the variable refreshedContactCount coming from?

@Seferi
Copy link
Author

Seferi commented Jun 30, 2024

First method line 6, if isContactRefreshed true, it increases the refreshedContactCount and UI should update the text in the dialog like ' 1 of 5 contacts are refreshed' to show the progress.

Project is open source, here is the source code: https://codeberg.org/Berker/keyoxide-flutter/src/branch/offline_support

and here is the sections of the code:

https://codeberg.org/Berker/keyoxide-flutter/src/branch/offline_support/lib/features/contacts/cubit/contacts_cubit.dart

https://codeberg.org/Berker/keyoxide-flutter/src/branch/offline_support/lib/features/contacts/widget/refresh_all_contacts_overlay.dart

@felangel
Copy link
Owner

felangel commented Jul 2, 2024

Hi @Seferi 👋
Thanks for opening an issue!

Are you able to create a simple, minimal reproduction sample? It would be much easier to help if you could isolate the issue and provide a super simple, example with minimal dependencies, thanks!

@felangel felangel added question Further information is requested waiting for response Waiting for follow up labels Jul 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working question Further information is requested waiting for response Waiting for follow up
Projects
None yet
Development

No branches or pull requests

3 participants