Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Consider canceling any active work during "dispose" or using the "mounted" getter to determine if the State is still active in Flutter

In the initState() method, I use requests to the server when the page is opened, and there is also an addListener method that listens for changes to the variable and calls methods if it has changed. But I encountered a small error that appears in the terminal when addListener works for me, the application continues to work but a warning constantly appears (I attached a screenshot below). Can you tell me how to get rid of this warning and this is an error, most likely because I do not close addListener?

@override
  void initState() {
    final appState = Provider.of<AppState>(context, listen: false);
    final ProfileCubit cubit = BlocProvider.of<ProfileCubit>(context);
    final CurrentEmailCubit currentEmailCubit =
        BlocProvider.of<CurrentEmailCubit>(context);
 
      myNotifier.addListener(() {
        if (myNotifier.value?.code != null) {
          SchedulerBinding.instance.addPostFrameCallback((_) async {
            final CurrentEmailCubit currentEmailCubit =
                BlocProvider.of<CurrentEmailCubit>(context);
            await currentEmailCubit.getCurrentEmail();
            currentEmailCubit
                .confirmEmail(
                    code: myNotifier.value?.code ?? '',
                    email: currentEmailCubit.currentEmail)
                .then((value) {
              CodeEmailVerif.code = null;
              myNotifier.value = CodeVerif(value: null);
              if (value) {
                cubit.fetchProfile(context).then((value) {
                  final profileState = cubit.state;
                  if (profileState is ProfileLoaded) {
                    appState.isEmailVerif = profileState.user!.isEmailConfirmed;
                    if (appState.isEmailVerif) {
                      _emailSuccessVerifDialog();
                    }
                  }
                });
              } 
            });
          });
        } 
      });
 
    if (CodeEmailVerif.code != null) {
            final CurrentEmailCubit currentEmailCubit =
                BlocProvider.of<CurrentEmailCubit>(context);
            await currentEmailCubit.getCurrentEmail();
            currentEmailCubit
                .confirmEmail(
                    code: CodeEmailVerif.code ?? '',
                    email: currentEmailCubit.currentEmail)
                .then((value) {
              CodeEmailVerif.code = null;
              myNotifier.value = CodeVerif(value: null);
              if (value) {
                cubit.fetchProfile(context).then((value) {
                  final profileState = cubit.state;
                  if (profileState is ProfileLoaded) {
                    appState.isEmailVerif = profileState.user!.isEmailConfirmed;
                    if (appState.isEmailVerif) {
                      _emailSuccessVerifDialog();
                    }
                  }
                });
              } 
            });
          });
    } else {
        SchedulerBinding.instance.addPostFrameCallback((_) async {
            await currentEmailCubit.getCurrentEmail();
            await cubit.fetchProfile(context).then((value) {
              final profileState = cubit.state;
              if (profileState is ProfileLoaded) {
                appState.isEmailVerif = profileState.user!.isEmailConfirmed;
                if (!appState.isEmailVerif) {
                  _emailDialog();
                } 
              }
            });
          });
    }
    initConnectivity();
    _connectivitySubscription =
        _connectivity.onConnectivityChanged.listen(_updateConnectionStatus);
    super.initState();
  }

  @override
  void dispose() {
    _connectivitySubscription.cancel();
    super.dispose();
  }

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Add mounted check between async operation and updating ui. Because your widget can be already disposed during async operation.

await myAsyncFunction();
if (mounted) {
  updateUI();
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading