Home > front end >  how can i get a number from a get method from an api and display it in a text widget in flutter (my
how can i get a number from a get method from an api and display it in a text widget in flutter (my

Time:09-21

Future<String> getNumberIntervention() async {
  final response = await http.get(
      Uri.parse('my url'));
  if (response.statusCode == 200) {
    
    String jsonResponse = json.decode(response.body);
    return jsonResponse.toString();
  } else {
    
    throw Exception('Failed to load Clients');
  }
}

when i call my api it returns number 2 without brakets so it's a no json format
how can i read this data and display it in a text widget in the dashboard

CodePudding user response:

You can use FutureBuilder widget. Try it

FutureBuilder(
  future: getNumberIntervention(),
  builder: (BuildContext context, AsyncSnapshot<String> snapshot) {
    if (snapshot.hasData) {
      return Text(snapshot.data!);
    }
    return const Text('Carregando');
  },
)

CodePudding user response:

You have to use state management. I prefer GetX state management.

  • Related