0

I wrote code to fetch data and put them to GridView Buider.

But condition snapshot.hasData not working, its always False and return CircularProgressIndicator().

But If I change condition to !snapshot.hasData(true) my code is working, and fetch data shows on the screen correctly.

Fetch API


List<String> pcBusy = [];
Future fetchDataStandart() async {
  final urlAuth =
      Uri.parse('http://xxx.xx.xxx.xxx/api/usersessions/activeinfo');
  final response = await http
      .get(urlAuth, headers: <String, String>{'authorization': basicAuth});

  if (response.statusCode == 200) {
    List listPc = List.from(json.decode(response.body)['result']);
    pcBusy.clear();
    for (int i = 0; i < listPc.length; i++) {
      pcBusy.add(listPc[i]['hostName']);
    }
    print(pcBusy);
  } else {
    throw Exception('Error');
  }

Builder code

Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: Text(
          'ЕВРОКО Стандарт',
        ),
      ),
      body: FutureBuilder(
        future: fetchDataStandart(),
        builder: (context, AsyncSnapshot snapshot) {
          if (snapshot.hasData) {
            return ComputerGrid();
          } else {
            return Center(
              child: CircularProgressIndicator(),
            );
          }
Чак Джонс
  • 115
  • 1
  • 1
  • 10
  • If you want to data from API refer my answer [here](https://stackoverflow.com/a/68533647/13997210) or [here](https://stackoverflow.com/a/68709502/13997210) or [here](https://stackoverflow.com/a/68594656/13997210) in this examples I have used snapshot.data hope it's helpful to you – Ravindra S. Patil Aug 15 '21 at 05:42

1 Answers1

1

Your fetchDataStandart() function does not have any return statement. Therefore calling it will not give you any data. You need to add a return statement. I do not know if you have just not shared the complete function, because one closing bracket is missing at the end.

TheUltimateOptimist
  • 1,089
  • 4
  • 19