Ahm*_*yed 5 sharedpreferences dart flutter
我正在使用 flutter 构建登录屏幕,并在共享首选项中保存用户数据(如果正确)\n然后我在第二个屏幕中测试了保存的数据,它已经保存\n当应用程序关闭并再次打开时,我检查共享pref 但它变成了 null\n那是我的 saveData 和 getData 函数
\n\nString namePref,phonePref, typePref,statePref;\nFuture saveDataPref(String name,String phone, String type, String state )async{\n SharedPreferences pref = await SharedPreferences.getInstance();\n pref.setString(\'phone\', phone);\n pref.setString(\'name\', name);\n pref.setString(\'type\', type);\n pref.setString(\'state\', state);\n pref.commit();\n print(\'pref stored\');\n print(\'${pref.getString(\'phone\')}\');\n}\n\nFuture getDataPref() async{\n SharedPreferences pref = await SharedPreferences.getInstance();\n phonePref = pref.getString(\'phone\');\n namePref = pref.getString(\'name\');\n typePref = pref.getString(\'type\');\n statePref = pref.getString(\'state\');\n}\nRun Code Online (Sandbox Code Playgroud)\n\n我应该提到两件事\n“提交”函数已被弃用\n第二件事是代码正在处理已弃用的提交,但现在不是\ni删除了我所做的所有更改,但它仍然无法在我的设备上工作\和其他设备,不知道为什么!\n如果没有解决方案,是否有其他方法可以替代 flutter 中的共享首选项?\n这就是我使用 saveDataPref() 的地方
\n\nFuture login(BuildContext context) async {\n String password = passwordController.text;\n String phone = phoneController.text;\n print("entered login function");\n //check if the user is registered\n Map<String, User> users = new Map<String, User>();\n users = await getUserDataFromFireStore();\n if (await userCheck(phone)) {\n // user is registered check if it is the right password\n if (users[phone].password == password) {\n // check if the user have an order\nRun Code Online (Sandbox Code Playgroud)\n\n\n\n\nsaveDataPref(\n 用户[电话].电话, 用户[电话].名称, "用户", 用户[电话].ordered);
\n
if (users[phone].ordered == "true") {\n Navigator.push(\n context, MaterialPageRoute(builder: (context) => OrderBinding()));\n return;\n } else {\n Navigator.push(\n context,\n MaterialPageRoute(\n builder: (context) => UserApp(\n userName: users[phone].name,\n userPhone: users[phone].phone,\n )));\n return;\n }\n } else {\n ShowDialogue(\n "\xd8\xaa\xd9\x85 \xd8\xa5\xd8\xaf\xd8\xae\xd8\xa7\xd9\x84 \xd9\x83\xd9\x84\xd9\x85\xd8\xa9 \xd8\xa7\xd9\x84\xd8\xb3\xd8\xb1 \xd8\xa8\xd8\xb4\xd9\x83\xd9\x84 \xd8\xae\xd8\xa7\xd8\xb7\xd8\xa6 \xd9\x85\xd9\x86 \xd9\x81\xd8\xb6\xd9\x84\xd9\x83 \xd8\xaa\xd8\xa3\xd9\x83\xd8\xaf \xd9\x85\xd9\x86 \xd9\x83\xd9\x84\xd9\x85\xd8\xa9 \xd8\xa7\xd9\x84\xd8\xb3\xd8\xb1", context);\n }\n }\n\n Map<String, Driver> drivers = new Map<String, Driver>();\n drivers = await getDriverDataFromFireStore();\n if (await DriverCheck(phone)) {\n // user is registered check if it is the right password\n if (drivers[phone].password == password) {\n //check if driver account is activated\n if (drivers[phone].activation == "true") {\n saveDataPref(drivers[phone].phone, drivers[phone].name, "driver",\n drivers[phone].activation);\n Navigator.push(\n context, MaterialPageRoute(builder: (context) => DriverApp()));\n } else {\n ShowDialogue(\n "\xd9\x84\xd9\x85 \xd9\x8a\xd8\xaa\xd9\x85 \xd8\xaa\xd9\x81\xd8\xb9\xd9\x8a\xd9\x84 \xd8\xad\xd8\xb3\xd8\xa7\xd8\xa8\xd9\x83\xd9\x85 \xd8\xa8\xd8\xb9\xd8\xaf \xd9\x8a\xd8\xb1\xd8\xac\xd9\x89 \xd8\xa7\xd9\x84\xd8\xa7\xd9\x86\xd8\xaa\xd8\xb8\xd8\xa7\xd8\xb1 \xd9\x88\xd8\xb3\xd9\x8a\xd8\xaa\xd9\x85 \xd8\xa7\xd9\x84\xd8\xaa\xd9\x88\xd8\xa7\xd8\xb5\xd9\x84 \xd9\x85\xd8\xb9\xd9\x83\xd9\x85 \xd9\x82\xd8\xb1\xd9\x8a\xd8\xa8\xd8\xa7\xd9\x8b",\n context);\n }\n } else {\n ShowDialogue(\n "\xd8\xaa\xd9\x85 \xd8\xa5\xd8\xaf\xd8\xae\xd8\xa7\xd9\x84 \xd9\x83\xd9\x84\xd9\x85\xd8\xa9 \xd8\xa7\xd9\x84\xd8\xb3\xd8\xb1 \xd8\xa8\xd8\xb4\xd9\x83\xd9\x84 \xd8\xae\xd8\xa7\xd8\xb7\xd8\xa6 \xd9\x85\xd9\x86 \xd9\x81\xd8\xb6\xd9\x84\xd9\x83 \xd8\xaa\xd8\xa3\xd9\x83\xd8\xaf \xd9\x85\xd9\x86 \xd9\x83\xd9\x84\xd9\x85\xd8\xa9 \xd8\xa7\xd9\x84\xd8\xb3\xd8\xb1", context);\n }\n } else {\n ShowDialogue(\n "\xd9\x84\xd9\x85 \xd9\x8a\xd8\xaa\xd9\x85 \xd8\xaa\xd8\xb3\xd8\xac\xd9\x8a\xd9\x84 \xd9\x87\xd8\xb0\xd8\xa7 \xd8\xa7\xd9\x84\xd9\x85\xd8\xb3\xd8\xaa\xd8\xae\xd8\xaf\xd9\x85 \xd9\x85\xd9\x86 \xd9\x81\xd8\xb6\xd9\x84\xd9\x83 \xd9\x82\xd9\x85 \xd8\xa8\xd8\xa7\xd9\x86\xd8\xb4\xd8\xa7\xd8\xa1 \xd8\xad\xd8\xb3\xd8\xa7\xd8\xa8 \xd8\xa7\xd9\x88\xd9\x84\xd8\xa7", context);\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n\n在这里我检查登录数据是否正确\n测试了该功能并且我工作\nit打开saveDataPref()\n并且我使用从另一个屏幕检索数据并且它工作\n但是当关闭应用程序并再次打开它时我使用getDataPref()但检索到的数据为空\n这里我使用 getDataPref() 来检查用户是否已经注册,所以转到 UserApp 屏幕,如果没有打开主屏幕
\n\ncheckSigned() {\n getDataPref();\n print("${phonePref}");\n print("${namePref}");\n if (phonePref != null) {\n print("${phonePref}");\n\n if (phonePref != "") {\n print("${phonePref}");\n\n return new UserApp(\n userName: namePref,\n userPhone: phonePref,\n );\n } else {\n return new MyHomePage();\n }\n } else {\n return new MyHomePage();\n }\n}\n\nvoid main() {\n runApp(new MyApp());\n}\n\nclass MyApp extends StatelessWidget {\n @override\n Widget build(BuildContext context) {\n return new MaterialApp(\n title: \'Generated App\',\n theme: new ThemeData(\n primarySwatch: Colors.lightGreen,\n primaryColor: const Color(0xFF8BC34A),\n accentColor: const Color(0xFF8BC34A),\n canvasColor: const Color(0xFFEEEEEE),\n ),\n home: checkSigned(),\n );\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n
我在这里找到了解决方案:
请注意,如果您像我之前那样在主类之外调用它,则共享首选项将不起作用。您应该在 initState() 内调用它。这有效:
@override
void initState() {
super.initState();
readFile('phone');
SharedPreferences.getInstance().then((SharedPreferences sp) {
sharedPreferences = sp;
ph = sp.getString('phone');
setState(() {});
});
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7831 次 |
| 最近记录: |