Flutter: Snapshot isn't showing data

337

in your EducationInfo.fromJson method

replace

  cgpa: json['cgpa'] ?? 0.0,
  outOfCgpa: json['out_of_cgpa'] ?? 0.0,
Share:
337
Abir Ahsan
Author by

Abir Ahsan

Currently, I am working as a Jr. flutter mobile app Developer. Contact to me +8801716422666

Updated on December 26, 2022

Comments

  • Abir Ahsan
    Abir Ahsan over 1 year

    I create a Listview builder with get method (API call). API call is fine, cause I get response. But in widget snapshot.data show me null. I can't fixed this problem and don't why it's behaving like this. Please someone help me.

    API responsebody

    enter image description here

    Here is my code for API call

    class APIService {
    
    Future<List<EducationInfo>> getEducationInfo() async {
        String url = "$baseAPIUrl/educations";
        String _token = await SavedData().loadToken();
        String authorization = "Bearer $_token";
        var response = await http.get(url, headers: {
          'Content-Type': 'application/json',
          'Accept': 'application/json',
          "Authorization": authorization
        });
        print('API ${response.statusCode}\n API${json.decode(response.body)}');
        if (response.statusCode == 200) {
          var jsonResponse = response.body;
          var decoded = json.decode(jsonResponse);
          return decoded['success']
              .map<EducationInfo>((b) => EducationInfo.fromJson(b))
              .toList();
        } else {
          throw Exception('Failed to load Education Information');
        }
      }
    }
    

    Here is my Model.dart

    //Model
    
    class EducationInfo {
      int id;
      String degreeName;
      int rollNumber;
      int regNumber;
      int passingYear;
      String gradeType;
      double cgpa;
      double outOfCgpa;
      String divisionName;
      String groupName;
      String subjectName;
      String boardName;
      String instituteName;
    
      EducationInfo({
        this.id,
        this.degreeName,
        this.rollNumber,
        this.regNumber,
        this.passingYear,
        this.gradeType,
        this.cgpa,
        this.outOfCgpa,
        this.divisionName,
        this.groupName,
        this.subjectName,
        this.boardName,
        this.instituteName,
      });
    
      factory EducationInfo.fromJson(Map<String, dynamic> json) {
        return EducationInfo(
          id: json['user_id'],
          degreeName: json['degree_name'],
          rollNumber: json['roll_number'],
          regNumber: json['registration_number'],
          passingYear: json['passing_year'],
          gradeType: json['grade_type'],
          cgpa: json['cgpa'],
          outOfCgpa: json['out_of_cgpa'],
          divisionName: json['division'],
          groupName: json['group_name'],
          subjectName: json['subject_name'],
          boardName: json['board'],
          instituteName: json['institute_name'],
        );
      }
    }
    

    And here is my main code-

    class Resume extends StatefulWidget {
      @override
      _ResumeState createState() => _ResumeState();
    }
    
    class _ResumeState extends State<Resume> {
      Future<List<EducationInfo>> furuteEducationInfo;
    
      @override
      void initState() {
        super.initState();
        furuteEducationInfo = APIService().getEducationInfo();
      }
      @override
      Widget build(BuildContext context) {
      return Scaffold(
          resizeToAvoidBottomPadding: false,
          appBar: AppBar(
            automaticallyImplyLeading: false,
            leading: IconButton(
              icon: Icon(
                Icons.arrow_back_ios,
              ),
              onPressed: () {
                Navigator.pop(context);
              },
            ),
            title: Text("Resume"),
          ),
          body: Align(
          child: FutureBuilder(
                                future: furuteEducationInfo,
                                  builder: (context, snapshot) {
                                    var educationInfo = snapshot.data;
                                    if (snapshot.data == null) {
                                      return Text("No Data Available ");
                                    } else if (snapshot.hasData) {
                                      return ListView.builder(
                                          scrollDirection: Axis.vertical,
                                          itemCount: educationInfo.length,
                                          itemBuilder: (context, index) {
                                            var eduInfo = educationInfo[index];
                                            print(
                                                "\nEducation Info ${educationInfo[index]}");
                                            return designedContainer(
                                                _width - 30,
                                                Padding(
                                                  padding: EdgeInsets.all(5.0),
                                                  child: Stack(
                                                    children: [
                                                      Container(
                                                        child: Column(
                                                          children: [
                                                            detailsField(
                                                                "Degree Name",
                                                                "${_checkNull(eduInfo.degreeName)}"),
                                                          ],
                                                        ),
                                                      ),
                                                      Align(
                                                        alignment:
                                                            Alignment.topRight,
                                                        child: editButton(
                                                            Icons.add, "Delete",
                                                            () {
                                                          print("Delete");
                                                        }),
                                                      )
                                                    ],
                                                  ),
                                                ));
                                          });
                                    } else {
                                      Text("Something Went to Wrong");
                                    }
                                  }),
           ),
        );
      }
    

    And here is also postman Screenshot-

    enter image description here

    • pskink
      pskink over 3 years
      add print(snapshot); before var educationInfo = snapshot.data; - what do you see on the logs?
    • Abir Ahsan
      Abir Ahsan over 3 years
      AsyncSnapshot<List<EducationInfo>>(ConnectionState.done, null, type 'int' is not a subtype of type 'double')
    • pskink
      pskink over 3 years
      so you have an error: snapshot.error is set to: "type 'int' is not a subtype of type 'double'"