1

I want to get the names from the json plaeholder api in flutter. But it is giving error, the following is my code:

    FutureBuilder(
          future: getuser(),
          builder: (context, snapshot){
if (snapshot.data==null) {
  return Container(
    child: Text("nothing"),
  );
}
else return ListView.builder(
  itemCount: snapshot.data.length,
  itemBuilder: (context,i){
    return ListTile(title: Text(snapshot.data[i].name),);  
    }
  );
          },
        )
7
  • If you get data from API refer my answer here or here or here hope it's helpful to you Commented Sep 10, 2021 at 10:45
  • @RavindraS.Patil It is giving error in item count that "The property 'length' can't be unconditionally accessed because the receiver can be 'null'." Commented Sep 10, 2021 at 10:50
  • can you share your API url I will test it? Commented Sep 10, 2021 at 10:52
  • jsonplaceholder.typicode.com/users Commented Sep 10, 2021 at 10:56
  • check my answer hope its help to you Commented Sep 10, 2021 at 11:05

1 Answer 1

2

Try below code I think your Problem has been solved and also add id and email also

Your API Call Function

Future<List<dynamic>> getJobsData() async {
    String url = 'https://jsonplaceholder.typicode.com/users';
    var response = await http.get(Uri.parse(url), headers: {
      'Content-Type': 'application/json',
      'Accept': 'application/json',
    });
    return json.decode(response.body);
  }

Your Widget

     Center(
          child: FutureBuilder<List<dynamic>>(
            future: getJobsData(),
            builder: (context, snapshot) {
              if (snapshot.hasData) {
                return Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: ListView.builder(
                    itemCount: snapshot.data.length,
                    itemBuilder: (context, index) {
                      var name = snapshot.data[index]['name'];
                      var email = snapshot.data[index]['email'];
                      var id = snapshot.data[index]['id'];
                      return Card(
                        shape: RoundedRectangleBorder(
                          side: BorderSide(color: Colors.green.shade300),
                          borderRadius: BorderRadius.circular(15.0),
                        ),
                        child: Column(
                          children: [
                            ListTile(
                              leading: Text(id.toString()),
                              title: Text(name),
                              subtitle: Text(email),
                            ),
                          ],
                        ),
                      );
                    },
                  ),
                );
              }
              return CircularProgressIndicator();
            },
          ),
        ),

Your Result Screen like -> enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.