1 回答

TA貢獻1816條經(jīng)驗 獲得超6個贊
我將向您展示我在項目中做了什么,首先您需要安裝Sharedprefrence,然后在 lib 文件夾中創(chuàng)建文件,創(chuàng)建名為 Utils 的文件夾,您可以提供任何您想要的名稱,并在 Utils 文件夾中創(chuàng)建一個文件 sharedpreference.dart?lib\Utils\
在此文件中的sharedpreference.dart中添加此行,ps:您可以使用此文件添加更多數(shù)據(jù),例如,如果api返回userid類型的內(nèi)容,您可以在此處指定,并且可以使用sharedprefrence在所有屏幕上訪問數(shù)據(jù)
class SharedPrefrence {
? ? ?Future<bool> setLoggedIn(bool status) async {
? ? ? ? ? ? final SharedPreferences prefs = await SharedPreferences.getInstance();
? ? ? ? ? ? return prefs.setBool("logged_in", status);
? ? ? ? ? }
? ? ? ??
? ? ? ? ? Future<bool> getLogedIn() async {
? ? ? ? ? ? final SharedPreferences prefs = await SharedPreferences.getInstance();
? ? ? ? ? ? return prefs.getBool("logged_in") ?? false;
? ? ? ? ? }
? ? ? ? Future<bool> setUserId(String userId) async {
? ? ? ? ? final SharedPreferences prefs = await SharedPreferences.getInstance();
? ? ? ? ? return prefs.setString("user_id", userId);
? ? ? ?}
? ??
? ? ? Future<String> getUserId() async {
? ? ? ? final SharedPreferences prefs = await SharedPreferences.getInstance();
? ? ? ? return prefs.getString("user_id") ?? '';
? ? ? }
}
登錄頁面
這是示例登錄功能,我在其中使用了首選項
void AppLogin(String username, String password) async {
? ? var response = await http.post(Urls.LOGIN,
? ? ? ? headers: {"Content-Type": "application/json"},
? ? ? ? body: json.encode({
? ? ? ? ? "User_Name": username,
? ? ? ? ? "Password": password,
? ? ? ? }));
? ? Map<String, dynamic> value = json.decode(response.body);
? ? if (response.statusCode == 200) {
? ? ? dialog.dismissProgressDialog(context);
? ? ? try {
? ? ? ? Map<String, dynamic> value = json.decode(response.body);
? ? ? ? SharedPrefrence().setLoggedIn(true);
? ? ? ? SharedPrefrence().setUserId(value['_id'].toString());
? ? ? ? Navigator.pushAndRemoveUntil(
? ? ? ? ? ? context,
? ? ? ? ? ? MaterialPageRoute(builder: (context) => DashboardScreen()),
? ? ? ? ? ? ModalRoute.withName("/login"));
? ? ? } catch (e) {
? ? ? ? e.toString();
? ? ? }
? ? }? else {
? ? ? dialog.dismissProgressDialog(context);
? ? ? var message = value['message'];
? ? ? CustomDialogs().showErrorAlert(context, message);
? ? }
? }
在您的初始屏幕中添加此函數(shù),并在 initState 函數(shù)中調(diào)用函數(shù) startTime,此時您的初始屏幕將顯示 3 秒,然后它將調(diào)用 navigationPage ,在其中檢查用戶是否登錄的登錄狀態(tài)的共享首選項如果沒有,它將顯示登錄信息,如果已登錄,它將重定向到 dahsboard 屏幕
?startTime() async {
? ? var _duration = new Duration(seconds: 3);
? ? return new Timer(_duration, navigationPage);
? }
? void navigationPage() {
? ? Future loginstatus = SharedPrefrence().getLogedIn();
? ? loginstatus.then((data) {
? ? ? if (data == true) {
? ? ? ? Navigator.pop(context, true);
? ? ? ? Navigator.pushAndRemoveUntil(
? ? ? ? ? ? context,
? ? ? ? ? ? MaterialPageRoute(builder: (context) => DashboardScreen()),
? ? ? ? ? ? ModalRoute.withName("/login"));
? ? ? } else {
? ? ? ? Navigator.pop(context, true);
? ? ? ? Navigator.push(
? ? ? ? ? context,
? ? ? ? ? MaterialPageRoute(
? ? ? ? ? ? builder: (context) => LoginScreen(),
? ? ? ? ? ),
? ? ? ? );
? ? ? }
? ? });
? }
- 1 回答
- 0 關注
- 121 瀏覽
添加回答
舉報