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 (将#修改为@)