How to query a database without trigger a button -flutter

Viewed 30

i have a ElevatedButton like this

ElevatedButton(
              onPressed: () {
                setState(
                  () {
                    cetak("SELECT CUST_NAME FROM ts_custxm ");
                  },
                );
              },
              child: Container(
                width: 200,
                child: Text(
                  "Cetak",
                  textAlign: TextAlign.center,
                ),
              ),
            )

and this is my Cetak() Method

  cetak(String query) async {
    var req = await SqlConn.readData(query);
    var parsedJson = jsonDecode(req);
    debugPrint(res.toString());
    setState(() {
      res = parsedJson;
    });
  }

The problem is, when i want to query using cetak() i must trigger my ElevatedButton

is there any ways to query without using some button?

also im using SQL_CONN dependencies

1 Answers

You can call it in initState(), now when you do this, before your page loaded cetak() have been called :

@override
  void initState() {
    super.initState();

    cetak("SELECT CUST_NAME FROM ts_custxm ");
    
  }
Related