Flutter-使用.Then(),提供商。F()和Navigator重定向到新屏幕

发布于 2025-01-19 09:25:14 字数 766 浏览 4 评论 0原文

我有一个按钮,在初始单击/点击/触摸时,它将其发布到API,然后根据AUTH返回True或False。

  onpressed:()=> {
提供者。
。
  if(value == true)
    navigator.pushreplacementamed(上下文,'/home')
  }),
},,
 
  Future< bool> login()async {
    尝试{
      var paryload = jsonencode({
        “电子邮件”:usrname,
        “密码”:PSWD
      });
      map< string,Dynamic>响应=等待fetch.post(url,正文:有效载荷,标头:{
        “接受”:'application/json',
        'content-type':'应用程序/json'
      }))
      。
      如果(响应['token']!=''){
        返回true;
      }
    }
    捕获(e){
      errormsg = e.tostring();
      notifyListeners();
      返回false;
    }
    返回false;
  }
 

在成功的身份验证中,它不会按预期重定向到新屏幕。我是否缺少某些东西,还是我的逻辑有缺陷?

I have a button which on initial click/tap/touch, it POST to API and return true or false based on auth.

onPressed: ()=>{
Provider.of<Login>(context, listen: false).login()
.then((value) => {
  if(value == true)
    Navigator.pushReplacementNamed(context, '/home')
  }),
},
Future<bool> login() async {
    try{
      var payload = jsonEncode({
        'email': usrname,
        'password': pswd
      });
      Map<String,dynamic> response = await fetch.post(url, body:payload, headers: {
        'Accept':'application/json',
        'content-type': 'application/json'
      })
      .then((value) => jsonDecode(value.body));
      if(response['token'] != ''){
        return true;
      }
    }
    catch(e){
      errorMsg = e.toString();
      notifyListeners();
      return false;
    }
    return false;
  }

On successful authentication, it doesn't redirect to new screen as intended. Am I missing something or my logic is flawed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

污味仙女 2025-01-26 09:25:14

await 旨在中断流程,直到 async 方法完成。然而,then 不会中断流程(意味着将执行下一条指令),但使您能够在异步方法完成时运行代码。

在您的代码中:

问题与您的 login 方法有关。您使用了 awaitthen,不需要同时使用两者。

Future<bool> login() async {
    try{
      var payload = jsonEncode({
        'email': usrname,
        'password': pswd
      });
      Map<String,dynamic> response = await fetch.post(url, body:payload, headers: {
        'Accept':'application/json',
        'content-type': 'application/json'
      });
      var decodedResponse = jsonDecode(response.body);
      if(decodedResponse['token'] != ''){
        return true;
      }
    }
    catch(e){
      errorMsg = e.toString();
      notifyListeners();
      return false;
    }
    return false;
  }

您可以在 SO issues 中找到更多示例:

  • Async/Await/then in Dart/Flutter
  • <一个href="https://stackoverflow.com/questions/60575776/futurevoid-async-await-then-catcherror-in-flutter-dart">Flutter/Dart 中的 Future、async、await、then、catchError

await is meant to interrupt the process flow until the async method has finished. then however does not interrupt the process flow (meaning the next instructions will be executed) but enables you to run code when the async method is finished.

In your code :

The issue is about your login method. You used an await with a then, you don't need to use both at the same time.

Future<bool> login() async {
    try{
      var payload = jsonEncode({
        'email': usrname,
        'password': pswd
      });
      Map<String,dynamic> response = await fetch.post(url, body:payload, headers: {
        'Accept':'application/json',
        'content-type': 'application/json'
      });
      var decodedResponse = jsonDecode(response.body);
      if(decodedResponse['token'] != ''){
        return true;
      }
    }
    catch(e){
      errorMsg = e.toString();
      notifyListeners();
      return false;
    }
    return false;
  }

You can find more examples in SO issues :

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文