0
votes

I'm new to Flutter and I';ve been trying out developing an app with a PHP API backend. Throughout countless of answers and online research, I'm unable to find a solution to the problem I'm facing I'm hoping someone could give me an input on what I'm doing wrong.

I'm trying to get a data form my API to display in a card. Below is my code:

...previous unnecessary code

      Padding(
          padding: EdgeInsets.symmetric(horizontal: 25.0, vertical: 30.0),
          child: Text('Summary Report',
              style: TextStyle(
                  color: Colors.black.withOpacity(0.7),
                  fontWeight: FontWeight.bold,
                  fontSize: 20.0))),
      Padding(
        padding: EdgeInsets.only(left: 15.0, bottom: 25.0, right: 15.0),
        child: Container(
          height: 150.0,
          child: ListView(
            scrollDirection: Axis.horizontal,
            children: <Widget>[
              UpcomingCard(
                  title: 'Total Service Calls',
                  value: _getTotalCalls(), //this is where I want to display my data
                  color: Colors.blue),
              UpcomingCard(
                  title: 'Pending Service Calls',
                  value: 0,
                  color: Colors.red),
            ],
          ),
        ),
... other unnecessary code

//function to get data
Future<int> _getTotalCalls() async {
  SharedPreferences localStorage = await SharedPreferences.getInstance();
  var user_id = localStorage.getString('user_id');
  var data = {'user_id': user_id};
  var res = await CallApi().postData(data, 'service/total');
  var body = json.decode(res.body);
  if (body['success']) {
    return body['servicecall_total'];
  } else {
    return 0;
  }
}

Hoping to get some help on this. Thank you

1

1 Answers

1
votes

When you want to display the result of a future function you need to use FutureBuilder

FutureBuilder<int>(
  future: _getTotalCalls(),
  initalData: 0,
  builder: (context, snapshot) {
    return UpcomingCard(
              title: 'Total Service Calls',
              value: snapshot.data.toString(), 
              color: Colors.blue)
  }
)