如何解决Flutter Session 存储数据但无法根据数据做出决策
我使用 FlutterSession 来存储电话号码/电子邮件(根据用户的选择)作为登录应用程序的一种方法。会话在电子邮件的情况下工作正常,但当我存储和检索电话号码时,它显示我的快照没有数据。
这是我为电话号码设置会话值的方法:
void phoneSignIn() async {
if (phoneNumber == null || phoneNumber.length < 10) {
print('Please enter a valid phone number!');
}
else {
await fillers.session.set("phoneNum",phoneNumber);
print('Phone number is: '+ phoneNumber);
setState(() {
phoneSignedIn = true;
});
var sessionCheck = await fillers.session.get("phoneNum");
print("Session value: $sessionCheck");
print(phoneSignedIn);
Navigator.pushReplacement(context,MaterialPageRoute(builder: (context) => OnboardOne()));
}
}
在上面的代码中,我使用打印语句确保会话存储电话号码值,并且 phoneSignedIn bool 设置为 true。
这是我为电子邮件设置会话值的方法:
void signIn() async {
response = await Dio().post(url,data: {
"email": emailVal,"password": passwordVal
}
);
if (response.statusCode == 200) {
await fillers.session.set('email',emailVal);
await fillers.session.set('password',passwordVal);
print('Sign In successful!');
print('Email: $emailVal');
print('Password: $passwordVal');
Navigator.pushReplacement(context,MaterialPageRoute(builder: (context) => OnboardOne()));
}
else
{
print('Exited with statuscode ${response.statusCode}');
print('Email: $emailVal');
print('Password: $passwordVal');
}
}
这是我的第一个页面,我根据用户是否登录来决定去哪个页面:
class _FirstPageState extends State<FirstPage> {
@override
Widget build(BuildContext context) {
return Material(
child: FutureBuilder(
future: phoneSignedIn ? session.get("phoneNum") : session.get('email'),builder: (context,snapshot) {
return snapshot.hasData ? Dashboard() : SignUpIn();
}
),);
}
}
如您所见,我在电子邮件登录方面没有做任何不同的事情(除了使用后端 api 对电子邮件进行身份验证)。最终,我使用相同的技术来存储电子邮件和电话号码。但是当我使用电话号码登录然后重新打开应用程序时,应用程序会转到 SignUpIn() 页面,而实际上它应该转到 Dashboard() 页面。 此外,在仪表板上有一个“退出”按钮,按下该按钮后,会话值将被清除。
我做错了什么?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。