0
votes

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(),
            );
          }
1
If you want to data from API refer my answer here or here or here in this examples I have used snapshot.data hope it's helpful to you - Ravindra S. Patil

1 Answers

1
votes

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.