2

My application has a user token and it expires after some time the user login. I got response code 401 and want to navigate to the login page, I am using dio and flutter_secure_storage to save tokens.

else if (e.response?.statusCode == 401) {
  //here I want to show the dialog box or navigate to the login page
  }
  print(getDioException(e));
  throw Exception(getDioException(e));
}
Anee
  • 49
  • 6

2 Answers2

0

If you want to show popup please do like below.

else if (e.response?.statusCode == 401) {
    showPopUpAlert();//here you have to call your popup function
  }
  print(getDioException(e));
  throw Exception(getDioException(e));
}

//popup UI implementation

showPopUpAlert() {
    showDialog(
        context: context,
        builder: (_) => AlertDialog(
          backgroundColor: Theme.of(context).scaffoldBackgroundColor,
          shape: const RoundedRectangleBorder(
              borderRadius: BorderRadius.all(Radius.circular(10.0))),
          content: Builder(builder: (context) {
            return StatefulBuilder(builder: (context, stateMaintain) {
              return SizedBox(
                width: Responsive.isMobile(context)
                    ? Responsive.screenWidth(context) / 1.1
                    : Responsive.screenWidth(context) / 2,
                height: 350,
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: <Widget>[
                    AutoSizeText('Popup text',
                        style:
                        Theme.of(context).textTheme.headline1),
                  ],
                ),
              );
            });
          }),
        ));
  }
harizh
  • 326
  • 1
  • 13
0

After some time of research, I found the answer. we need to create a global key for navigating and after we got the 401 we need to navigate the login page. define global key final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>(); pass the value in MaterialApp

navigatorKey: navigatorKey,

and use into your class

navigatorKey.currentState?.pushNamed('/LoginScreen');
Anee
  • 49
  • 6