void initState()Flutter和php不起作用

0yycz8jy  于 2023-04-04  发布在  PHP
关注(0)|答案(1)|浏览(78)

我已经开发出当我登录时,工作只是为了访问..
访问后不工作,看看名字和姓氏谁是登录...
我写代码…
Flutter

initState() {
    super.initState();
    getData();
  }

void getData() async {

    final SharedPreferences prefs = await SharedPreferences.getInstance();
    code = prefs.getString('code');

    var url = "http://localhost/profilo.php";

    var response = await http.post(Uri.parse(url), body: {
      "username": code,
    });

    var data = jsonDecode(response.body);

    nomecognome = "WELCOME "+ data['name'] +" "+ data['surname'];

    print(nomecognome);

  }


  @override
  Widget build(BuildContext context) {
    return WillPopScope(
        onWillPop: _onWillPop,
        child: Scaffold(
      body: Container(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget> [
              SizedBox(height: 50,),
              Padding(padding: EdgeInsets.all(20),
                  child: Center(
                    child: Text(nomecognome ?? "", style: TextStyle(color: Colors.grey, fontSize: 20),),
                  )
              ),
              TextButton(onPressed: () {
                esciLogout();
              },
                child: Text("LOGOUT",
                  style: TextStyle(fontFamily: "NovareseStd",
                      color: Colors.grey,
                      fontSize: 18),
                ),
              ),
            ],
          ),
        ),
        ),
    );
  }
}

PHP

<?php

$queryCheck = "SELECT *  FROM utents WHERE username='". strtolower($_REQUEST['username']) ."'";
    $selectCheck = mysqli_query($connessione, $queryCheck);
    
    $result = array();

    $resultCheck = mysqli_fetch_array($selectCheck);
        
    $result['name'] = $resultCheck['name'];
    
    $result['surname'] = $resultCheck['surname'];

    echo json_encode($result);

?>

字符串CODE工作,当我调试时,在终端工作中看到“flutter:欢迎演示哈维”......是好的,但我不明白为什么在孩子:文本不显示?
我做错了什么?还是少了什么?
非常感谢
我想当我登录,是成功的,下一页必须出现“欢迎演示哈维”的文本或标签

tkclm6bt

tkclm6bt1#

在你的getDate函数中,你应该调用setState((){})来让flutter知道它应该重建小部件。下面是更新后的解决方案:

void getData() async {

    final SharedPreferences prefs = await SharedPreferences.getInstance();
    code = prefs.getString('code');

    var url = "http://localhost/profilo.php";

    var response = await http.post(Uri.parse(url), body: {
      "username": code,
    });

    var data = jsonDecode(response.body);

    nomecognome = "WELCOME "+ data['name'] +" "+ data['surname'];
    setState((){}); //Add this line
    print(nomecognome);

  }

相关问题