Home > Blockchain >  How to repeatedly call methods in initState without reloading the page?
How to repeatedly call methods in initState without reloading the page?

Time:07-29

I have a page with initState method. When I open the page, I run requests in the initState method. In requests, I use the CodeEmailVerif.code global variable. Tell me, if I change the value of a variable, how can I tell it and call methods in initState again without reloading the page?

main

@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);

      if (CodeEmailVerif.code != null) {
        SchedulerBinding.instance.addPostFrameCallback((_) async {
          final CurrentEmailCubit currentEmailCubit =
              BlocProvider.of<CurrentEmailCubit>(context);
          await currentEmailCubit.getCurrentEmail();
          currentEmailCubit
              .confirmEmail(
                  code: widget.code ?? '',
                  email: currentEmailCubit.currentEmail)
              .then((value) {
            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();
              } 
            }
          });
        });
      }
    super.initState();
  }

  @override
  Widget build(BuildContext context) {

global_variable

class CodeEmailVerif {
  static String? code;
}

CodePudding user response:

Just put the code that is in initState in a method you create, call this method from initState and from anywhere else you need.

CodePudding user response:

I think skip 1st three line, but if you need, you can wrap everything inside listener, I am going to use ValueNotifier with modifying the class. Also, I don't think you can skip using static variable for this case.

class CodeEmailVerif {
  String? _code;
  String? get code => _code;

  CodeEmailVerif({String? value}) {
    _code = value;
  }
}

final ValueNotifier<CodeEmailVerif?> myNotifier = ValueNotifier(
  CodeEmailVerif(),
);

To update value use

 myNotifier.value = CodeEmailVerif(value: count.toString());
 @override
  void initState() {
    myNotifier.addListener(() {
      final appState = Provider.of<AppState>(context, listen: false);
      final ProfileCubit cubit = BlocProvider.of<ProfileCubit>(context);
      final CurrentEmailCubit currentEmailCubit =
          BlocProvider.of<CurrentEmailCubit>(context);
      //* you can put theses above listner
      if (myNotifier.value?.code != null) {
        SchedulerBinding.instance.addPostFrameCallback((_) async {
          final CurrentEmailCubit currentEmailCubit =
              BlocProvider.of<CurrentEmailCubit>(context);
          await currentEmailCubit.getCurrentEmail();
          currentEmailCubit
              .confirmEmail(
                  code: widget.code ?? '',
                  email: currentEmailCubit.currentEmail)
              .then((value) {
            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();
              }
            }
          });
        });
      }
      super.initState();
    });
  }
  • Related