Why snapshot.connectionState in FutureBuilder is directly going to ConnectionState.done in the Below code

31 Views Asked by At
ResultAPI resultsAPI = ResultAPI();
  @override
  Widget build(BuildContext context) {
    return Material(
      child: FutureBuilder(
        future: resultsAPI.getResult(2081951044),
        builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot)
        {
          if(snapshot.connectionState == ConnectionState.waiting)
          {
            return AlertDialog(
              title : Text("${snapshot.connectionState}"),
            );
          }
          else if(snapshot.connectionState == ConnectionState.done)
            {
              return AlertDialog(
                title: Text("${snapshot.data}"),
              );
            }
          else
            {
              return AlertDialog(
                title : Text("${snapshot.connectionState} ${snapshot.data}"),
              );
            }
        },
      ),
    );
  }
late StudentInfo studentInfo;

late ResultInfo resultInfo;
static List student = [];
static List result =[];
static List studentResultDetails = [];
Future getResult(var reg_no) async
{
  final url = "https://springbootdemo-production-70a9.up.railway.app/result/${reg_no}";
  final uri = Uri.parse(url);
  print("Request Sent");
  var response = await http.get(uri);
  var body = jsonDecode(response.body);
  print(body);
  print("Body Parsed");
  // Student Data
  studentInfo = StudentInfo();
  studentInfo.setStudentId(body["student_id"]);
  studentInfo.setStudentName(body["student_name"]);
  studentInfo.setBranch(body["branch"]);
  student.add(studentInfo);
  print(student[0].getStudentName());
  // Student Result
  for(var i = 1; i <= 4; i++)
    {
      resultInfo = ResultInfo();
      resultInfo.setPapCode(body["results"]["pap${i}c"]);
      resultInfo.setPapName(body["results"]["pap${i}n"]);
      resultInfo.setPapGPA(body["results"]["pap${i}gr"]);
      result.add(resultInfo);
    }
  print("Results Fetched");
  studentResultDetails.add(student);
  studentResultDetails.add(result);
  return studentResultDetails;
}

This is the Output :

Request Sent

ConnectionState.waiting

ConnectionState.done

Why I didn't get the response from the API?

The program is for getting the results of student from database and fetching it into UI .

The await http.get() is not called.

Please check the above given code

0

There are 0 best solutions below