I created a login form in where i am passing the username to dashboard screen using shared perference, problem is when i logged in from abc
name and pass it to dashboard it is displaying correct name, but when i logged in from xyz
name it display the abc
name on dashboard, it is not reseting the variable value, i run the app again, hot reload each and everything but noting happend.
here is the code of login screen
String getname="";
Future login() async {
Dio dio = new Dio();
var myPrefs = await SharedPreferences.getInstance();
data = {
'username':"abc",
'password': "abc123",
'date': formattedDate
};
await dio.post(localhostUrlLogin,data: json.encode(data),)
.then((onResponse) async {
getname = (onResponse.data['User']['username']);
}).catchError((onerror) {});
await myPrefs.setString('name', getname);
Navigator.push(
context, new MaterialPageRoute(builder: (context) =>dashboard()));
}
dashboard screen code
class dashboard extends StatefulWidget {
@override
_dashboard State createState() => _dashboard State();
}
class _dashboard State extends State<dashboard > {
String getname = "";
_userDetails() async {
SharedPreferences myPrefs = await SharedPreferences.getInstance();
setState(() {
getname = myPrefs.getString('name');
}
void initState() {
super.initState();
_userDetails();
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.grey[200],
appBar: new MyAppBar(title: Text("Home")),
drawer: NavigationDrawerWidget(),
body: SingleChildScrollView(
child: Column(children: [
SizedBox(height: 10),
Center(
child: Text(
"Hello, " + getname,
style: TextStyle(fontSize: 35, fontWeight: FontWeight.bold),
),
),
}
please help how to fix it.