I was making an app using flutter and i have used shared_preferences package, and in auth stage i am facing an issue where when i build app user is logged in and when i log out and restart the app after killing it ,it still goes on homepage , Here is my code
main.dart
bool checkingKey;
Future<bool> checkKey() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
bool checkingKey=prefs.containsKey("jwt");
print("$checkingKey");
return checkingKey;
}
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
Paint.enableDithering = true;
await checkKey().then((value){
checkingKey=value;
});
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
// bool check=checkKey().then((bool value) => true);
print("hello=$checkingKey");
return MaterialApp(
home: AnnotatedRegion<SystemUiOverlayStyle>(
value: SystemUiOverlayStyle(
statusBarColor: Colors.transparent,
),
child: Scaffold(
resizeToAvoidBottomInset: false,
body: Container(
color: Color(0xffccffcc),
child:checkingKey==false?LoginPage():mainPage()
),
),
),
routes: <String,WidgetBuilder>{
'/home':(BuildContext context)=>mainPage(),
'/login':(BuildContext context)=>LoginPage(),
}
);
}
}
login_signup_Auth.dart
Future<void> attemptLogIn(String username, String password,BuildContext context) async {
///?final storage =parent_inherit.of(context);
///?var verify=storage.verify;
SharedPreferences prefs = await SharedPreferences.getInstance();
print("$username $password");
final http.Response res = await http.post(
"https://green-earth.herokuapp.com/signin",
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
// 'authorization':'Bearer '+
},
body: jsonEncode(<String, String>{
"email": username,
"password": password
}),
);
if(res.statusCode == 200) {
prefs.setString('jwt',res.body);
var value=prefs.getString('jwt');
print("storage= ${value.isEmpty}");
Navigator.of(context).pushNamed('/home');
}
else{
return _showMyDialoglogin(context,res.statusCode);
}
}
void logoutOutOfApp(BuildContext context) async{
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.clear();
Navigator.of(context).pushNamedAndRemoveUntil('/login', (Route<dynamic> route) => false);
}
On the second build without changing anything , the checking key variable is returned 'true' which i don't know ,how can it be possible!!!!!!
I am not getting what i am doing wrong ,also if u see any other problem which can make program efficient or any other code which shall be used .please tell
ThankYou very much!!
Why you are complicating things ?
Your main.dart
can simply looks like this
bool checkingKey;
void main() async {
WidgetsFlutterBinding.ensureInitialized();
Paint.enableDithering = true;
var prefs = await SharedPreferences.getInstance();
checkingKey = prefs.containsKey("jwt");
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
print("hello=$checkingKey");
return MaterialApp(
home: AnnotatedRegion<SystemUiOverlayStyle>(
value: SystemUiOverlayStyle(
statusBarColor: Colors.transparent,
),
child: Scaffold(
resizeToAvoidBottomInset: false,
body: Container(
color: Color(0xffccffcc),
child: !checkingKey ? LoginPage() : mainPage(),
),
),
),
routes: <String,WidgetBuilder>{
'/home':(BuildContext context) => mainPage(),
'/login':(BuildContext context) => LoginPage(),
},
);
}
}