-1

Here is the Future function that should return the data in my php file

    Future displayLg_name() async {
        var url = Uri.http(
            "192.168.68.216", '/ump_attendance_2/username.php', {'q': '{http}'});
        var response = await http.post(url, body: {
          "lg_username": username.lg_username,
        });
        if (response.body.isNotEmpty){
          return (json.decode(response.body));
        };

I'm trying to display it in my container

   Container(
          alignment: Alignment.centerLeft,
          child: Text(
            displayLg_name().toString(),
            style: TextStyle(
              color: Colors.black54,
              fontFamily: "Ubuntu",
              fontWeight: FontWeight.bold,
              fontSize: scrwidth / 18,
            ),
          ),
        ),

And here is my php file

            <?php
            include ("config.php");
            session_start();
            $lg_username = $_REQUEST['lg_username'];

            $sql = "SELECT lg_name FROM hr_login WHERE lg_username = '$lg_username'";

            $res = mysqli_query($conn,$sql);

            if (mysqli_num_rows($res)>0){
                while($row = mysqli_fetch_assoc($res)){
                    echo json_encode($row['lg_name']);
                }
            }
            ?>

I'm guessing there's nothing wrong with my php file but somewhere in flutter .dart

Thanks in advance

  • **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/32391315) – Dharman Jun 20 '22 at 10:40
  • my bad. I accidentally put to much attention to solve error in flutter.. thanks for letting me know! – Jasmirulnaim Jun 21 '22 at 01:26

1 Answers1

0

Try to do something like this instead:

var text = await displayLg_name();
/* ... */
Container(
  alignment: Alignment.centerLeft,
  child: Text(
    text ?? '',
    style: TextStyle(
      color: Colors.black54,
      fontFamily: "Ubuntu",
      fontWeight: FontWeight.bold,
      fontSize: scrwidth / 18,
    ),
  ),
), 

Edited
If your class is statefull, you can declare it inside initState() like this:

var text;

void _init() async {
  await displayLg_name().then((value) => setState(() => text = value));
}

@override
void initState() {
  super.initState();
  _init();
}
belinda.g.freitas
  • 1,016
  • 3
  • 7
  • 15