0

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.

TimeToCode
  • 1,458
  • 3
  • 18
  • 60
  • 1
    In your code snippet you have a hardcoded data object, which you always set in shared preferences. Is that just for the example here or in the code itself? – tomerpacific Sep 07 '21 at 05:48
  • i hard code this value ```'username':"abc",``` just for the sake of fast developing, but when i change abc to xyz the problem occur, if i don't hard code the values then still the same issue, so i guess hard coded values are not the issue – TimeToCode Sep 07 '21 at 05:55
  • @tomerpacific infact, it was working perfectly few days before, but now it is creating issue! – TimeToCode Sep 07 '21 at 05:58

0 Answers0