Ananda Pramono
Ananda Pramono

Reputation: 1009

Expected a value of type 'String', but got one of type 'Null' in FutureBuilder SnapShot Flutter

I'm new in FLutter and try to get some JSON data and show it in a List. When I tried to connect to the APi, it works just fine, and I got the JSON response from the server. But when I try to show it in the list with FutureBuilder, it says that the snapshot returns null, even thought the response from the data is exist. This is the JSON response that I got from the server

{
            "error_code"     : "00",
            "error_message"  : "Success",
            "user_list"       : 
            [
                {
                    "name"   : "Swift",
                    "user_id": "2048"
                }, {
                    "name"   : "Python",
                    "user_id": "1024"
                }, {
                    "name"   : "Objective-C",
                    "user_id": "512"
                }, {
                    "name"   : "Ruby",
                    "user_id": "256"
                }
            ]
        }

This is my get method, because I only want to use the user_list field, I try to filter it this way.

Future<List<Patient>> getPatient() async {
    final response = await http.get(Uri.parse(url+"/interns"));
    if (response.statusCode == 200) {
        var data = json.decode(response.body);
        print(data['user_list']);
        return List<Patient>.from(data['user_list'].map((item)=>Patient.fromJson(item)));
    } else {
        throw Exception("Failed");
    }
  }

This is the FutureBuilder that throw the error

FutureBuilder<List<Patient>>(
                future: patientService.getPatient(),
                builder: (BuildContext context, AsyncSnapshot snapshot) {
                  if(snapshot.hasError) {
                      print(snapshot);
                      return Center(
                        child: Text("Error"),
                      );
                  } else if (snapshot.hasData){
                      List<Patient> patients = snapshot.data;
                      return _buildListView(patients);
                  } else {
                      return Center(
                      child: Container(),
                    );
                  }
                },
              ),

And this is the patient model class

Patient({required this.name, required this.userId});

    factory Patient.fromJson(Map<String, dynamic> json) {
      return Patient(
          name    :json['name'],
          userId  : json['userId']
      );
    }

This is my error

AsyncSnapshot<List<Patient>>(ConnectionState.done, null, Expected a value of type 'String', but got one of type 'Null'

Upvotes: 0

Views: 1530

Answers (2)

Marius Popescu
Marius Popescu

Reputation: 483

As John mentioned in this post, to fix your code you need to use user_id when parsing the Patient data.

However, not sure if you fixed this issue in your code already, but the code that you shared with end up making a many extra calls to your endpoint because you are creating your Future getPatient in a Build method. If you put a breakpoint in your code, it should be called at least 3 times on a first app reload.

You should store the Future in a local variable and reference that in the FutureBuilder. This is a very common problem that I see people make and I also did this problem in the past.

Upvotes: 0

John Joe
John Joe

Reputation: 12803

It should be

json['user_id '] instead of json['userId']. The parameter name is different, that's why you are getting Null error.

Upvotes: 1

Related Questions