I have a simple app from which a user can login and on logging in, a token is generated and stored on the device.
When the app starts, the following code below runs.
import 'package:coolkicks/screens/authpage.dart';
import 'package:coolkicks/screens/homescreen.dart';
import 'package:flutter/material.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:logger/logger.dart';
class Authenticate extends StatefulWidget {
@override
_AuthenticateState createState() => _AuthenticateState();
}
class _AuthenticateState extends State<Authenticate> {
final storage = new FlutterSecureStorage();
var log = Logger();
bool authenticated = false;
void checkToken() async {
String token = await storage.read(key: 'token');
if (token == null || token.length == 0) {
authenticated = false;
} else {
authenticated = true;
print(token);
log.d(token);
log.i(token);
}
}
@override
Widget build(BuildContext context) {
//check if Authenticated or Not
//return either Products Home Screen or Authentication Page
//If token exists, return Home screen
//Else return authpage
checkToken();
if(authenticated) {
return HomeScreen();
}
else {
return AuthPage();
}
}
}
My issue is that retrieving the token returns a future and takes some time to execute.
So it always returns the default authenticated = false