为什么在Flutter中函数会无限调用?

huangapple go评论60阅读模式
英文:

why the function in flutter calling infinitely?

问题

我是Flutter新手。我在点击按钮时调用登录API。API代码写在下面的一个函数中。

我的问题是,点击按钮后,这个函数无限调用,一次只能调用一次。

Future<Login?> logins() async {
    print("login function called");
    String uemail = emailText.text;
    String upassword = passwordText.text;

    print("Email: $uemail, Pass: $upassword");

    if (uemail.isEmpty || uemail == null) {
        print('enter your email');
    } else if (upassword.isEmpty || upassword == null) {
        print('enter your password');
    } else {
        Uri myUri = Uri.parse("http://192.168.29.248:5000/login");
        var json_body = {'email': uemail, 'password': upassword};

        var response = await http.post(myUri, body: json_body);
        print('response code ' + response.statusCode.toString());
        if (response.statusCode == 200) {
            var res = json.decode(response.body);
            print('response res' + res.toString());
            login = Login.fromJson(res);
            if (login?.success == 0) {
                print('enter email');
            } else if (login?.success == 2) {
                print('enter password');
            } else if (login?.success == 1) {
                print('login success');
                Navigator.push(
                    context, MaterialPageRoute(builder: (context) => HomeScreen()));
            }
        }
        return logins();
    }
}

请查看我的代码并提供解决方案。

英文:

I'm new in flutter. I'm calling a login API on button click. The API code is written in a function which is below.

My problem is that this function is calling infinitely after clicking on button at a time only.

      Future&lt;Login?&gt; logins() async {
        print(&quot;login function called&quot;);
        String uemail = emailText.text;
        String upassword = passwordText.text;

        print(&quot;Email: $uemail, Pass: $upassword&quot;);

        if (uemail.isEmpty || uemail == null) {
          print(&#39;enter your email&#39;);
        } else if (upassword.isEmpty || upassword == null) {
          print(&#39;enter your password&#39;);
        } else {
          Uri myUri = Uri.parse(&quot;http://192.168.29.248:5000/login&quot;);
          var json_body = {&#39;email&#39;: uemail, &#39;password&#39;: upassword};

          var response = await http.post(myUri, body: json_body);
          print(&#39;response code &#39; + response.statusCode.toString());
          if (response.statusCode == 200) {
            var res = json.decode(response.body);
            print(&#39;response res&#39; + res.toString());
            login = Login.fromJson(res);
            if (login?.success == 0) {
              print(&#39;enter email&#39;);
            } else if (login?.success == 2) {
              print(&#39;enter password&#39;);
            } else if (login?.success == 1) {
              print(&#39;login success&#39;);
              Navigator.push(
                  context, MaterialPageRoute(builder: (context) =&gt; HomeScreen()));
            }
          }
          return logins();
        }
      }

Please view my code and provide the solution.

答案1

得分: 0

因为您正在调用函数 return logins();

这被称为递归函数。

根据您的代码,它将是

return login;
英文:

Because you are retuning(calling) the function return logins();

It is known as recursive function.

based on your code it will be

return login;

huangapple
  • 本文由 发表于 2023年3月4日 02:51:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75630837.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定