0

Scenario

I am trying to implement an auto-logout feature in my app and have been following the solution posted here.

However, instead of navigating directly to a particular logged-out screen I would like to show a dialog box, clicking on whose button will take the user to the said logged-out screen.

The following is my code inside the navToHomePage:

void navToHomePage(BuildContext context) {
log("Navigation to home page called");
// navigatorKey.currentState?.pushNamedAndRemoveUntil(
//   Routes.onboarding,
//   (Route<dynamic> route) => false,
//   arguments: OnboardingArgumentModel(isInitial: true).toMap(),
// );
showDialog(
  context: context,
  builder: (context) {
    return CustomDialog(
      svgAssetPath: ImageConstants.warning,
      title: "Session Timeout",
      message: "Your session has timed out.\nPlease login again.",
      actionWidget: GradientButton(
        onTap: () {
          navigatorKey.currentState?.pushNamedAndRemoveUntil(
            Routes.onboarding,
            (Route<dynamic> route) => false,
            arguments: OnboardingArgumentModel(isInitial: true).toMap(),
          );
        },
        text: "Login",
      ),
    );
  },
);  
}

Problem

Everything works as expected when I keep the commented out code instead of the showDialog. However, when I call the showDialog code I get the following error: No MaterialLocalizations found.

Request

I would like to know what is wrong in my code in the showDialog section which is causing this error and how to go about resolving this issue.

Ayan Dasgupta
  • 304
  • 1
  • 4
  • 11

1 Answers1

0

I added the following as my showDialog's context: navigatorKey.currentState!.overlay!.context and it got rid of the error. The solution was to resort to contextless calls.

Ayan Dasgupta
  • 304
  • 1
  • 4
  • 11