Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter how to redirect to login if not authorized

Tags:

flutter

I'm trying to redirect to login in case the token has been expired in Flutter

Trying to get the posts:

body: new Container(
    padding: new EdgeInsets.only(bottom: 8.0),
    color: Color(0xff2c3e4e),
    child: FutureBuilder<List<Post>>(
        future: api.getposts(),
        builder: (context, snapshot) {
            // doing stuff with response
        }
    )
)

getposts and catching the error:

Future<List<Post>> getposts() async {
    url = 'url';
    var response = await http.get(url,
        headers: {HttpHeaders.authorizationHeader: 'bearer ' + token},
    );
    //part I can't understand how to get to work, how do I push? This gives error about context not being defined.
    if (response.body.toString().contains('Token is Expired')) {
        Navigator.push(
            context,
            MaterialPageRoute(
                builder: (context) =>
                    LoginScreen()),
        );
    }
}

So the question is, how do I use the Navigator correctly in such cases and I can redirect back to loginScreen in case the token has been expired and needs to be renewed? Like mentioned in the comment in the code example, the context gives me "undefined".

Is this even possible the way I am doing it or am I simply handling the whole check completely wrong?

like image 989
EmJeiEn Avatar asked Mar 07 '19 22:03

EmJeiEn


1 Answers

Code should have single resonpsibility. Your getPost method are doing 2 things at the same time. You should break this function such that it either successfully get the the post, or throw exception, and its caller will handle the exception. Its caller btw must be within build method, because only build method has BuildContext context, something like this:

      if (response.body.toString().contains('Token is Expired')) {
        throw new Exception("Token is Expired") // you may want to implement different exception class
      }
      body: new Container(
        padding: new EdgeInsets.only(bottom: 8.0),
        color: Color(0xff2c3e4e),
        child: FutureBuilder<List<Post>>(
            future: api.getposts(),
            builder: (context, snapshot) {
              if (snapshot.hasError) {
                // you might want to handle different exception, such as token expires, no network, server down, etc.
                Navigator.push(
                  context,
                  MaterialPageRoute(builder: (context) => LoginScreen()),
                );
                return Container(); // return empty widget
              }
              if (snapshot.hasData) {
                // do somthing with data
                return Text('Some data here');
              }
              // has no data
              return Text("Loading...");

            }),
      ),

UPDATE

Thanks to @TruongSinh I got it figured out.

Followed his example and figured out the build navigator method which works:

if (snapshot.hasError) {

              @override
              void run() {
                scheduleMicrotask(() {

                  Navigator.pushReplacement(
                    context,
                    MaterialPageRoute(builder: (context) => LoginScreen()),
                  );
                });
              }
              run();
}
like image 135
TruongSinh Avatar answered Oct 15 '22 03:10

TruongSinh