登录后如何使用用户详细信息?

时间:2020-04-16 09:31:56

标签: authentication flutter dart

我创建了一些函数来通过rest api将用户登录到我的系统中。一切正常,但我还想使用有关我刚刚登录的用户的信息,并将其显示在主页或抽屉中,任何人都不知道该怎么做?

我的代码:

登录功能:

  signIn(String email, pass) async {
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    Map data = {
      'email': email,
      'password': pass
    };
    var jsonResponse = null;
    var response = await http.post("http://10.0.2.2:80/user/login", body: data);
    if(response.statusCode == 200) {
      jsonResponse = json.decode(response.body);
      if(jsonResponse != null) {
        setState(() {
          _isLoading = true;
        });
        sharedPreferences.setInt("id", jsonResponse['id']);
        Navigator.of(context).pushAndRemoveUntil(MaterialPageRoute(builder: (BuildContext context) => MainPage()), (Route<dynamic> route) => false);
      }
    }
    else {
      setState(() {
        _isLoading = false;
      });
      print(response.body);
    }
  }

成功登录系统后的主页:

class _MainPageState extends State<MainPage> {

  SharedPreferences sharedPreferences;

  @override
  void initState() {
    super.initState();
    checkLoginStatus();
  }

  checkLoginStatus() async {
    sharedPreferences = await SharedPreferences.getInstance();
    if(sharedPreferences.getInt("id") == null) {
      Navigator.of(context).pushAndRemoveUntil(MaterialPageRoute(builder: (BuildContext context) => LoginPage()), (Route<dynamic> route) => false);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Code Land", style: TextStyle(color: Colors.white)),
        actions: <Widget>[
          FlatButton(
            onPressed: () {
              sharedPreferences.clear();
              Navigator.of(context).pushAndRemoveUntil(MaterialPageRoute(builder: (BuildContext context) => LoginPage()), (Route<dynamic> route) => false);
            },
            child: Text("Log Out", style: TextStyle(color: Colors.white)),
          ),
        ],
      ),
      body: Center(child: Text("Main Page")),
      drawer: Drawer(),
    );
  }
}

我的用户对象:

  "id": 1,
  "firstName": "admin",
  "lastName": "admin",
  "accountName": "Kot filemon",
  "email": "admin@admin.pl",
  "active": false,
  "activateCode": 0,
  "admin": true,
  "latitude": 000,
  "longitude": 000,
  "profileImage": null

感谢您的帮助:)

1 个答案:

答案 0 :(得分:0)

这不是完整的详细答案,而是描述您可以遵循的过程的方式。


那么,您可以将用户信息存储在您的应用程序中。我看到您只保存 ID 并在此行中使用sharedPreferences

sharedPreferences.setInt("id", jsonResponse['id']);

您可以使用与 ID

相同的方式保存其他字段
sharedPreferences.setString("firstName", jsonResponse['firstName']);
sharedPreferences.setString("lastName", jsonResponse['lastName']);
sharedPreferences.setString("accountName", jsonResponse['accountName']);
sharedPreferences.setString("email", jsonResponse['email']);

然后,您需要在MainPage中以与函数checkLoginStatus()相同的方式恢复此数据。

User()需要一个类来组织数据。

class User {
  int id;
  String firstName;
  String email;

  User({this.id, this.firstName, this.email});
}

一个getUser()函数,用于使用User类检索数据

Future<User> getUser() async {
    sharedPreferences = await SharedPreferences.getInstance();

    var id = sharedPreferences.getInt("id");
    var firstName = sharedPreferences.getString("firstName");
    var email = sharedPreferences.getString("email");

    return User(id: id, firstName: firstName, email: email);
  }

并在您的initState()函数中,如下调用此函数:

  SharedPreferences sharedPreferences;
  Future<User> myUser;

  @override
  void initState() {
    super.initState();
    checkLoginStatus();
    myUser = getUser();
  }

该代码未经测试!