4

This is the response from request.

var response = [{"id":4731},{"id":4566},{"id":4336},{"id":4333},{"id":4172},{"id":4170},{"id":4168},{"id":4166},{"id":4163},{"id":4161}];

How to extract ids and store in List of int using flutter. I have try this code but not working.

Future<List<int>> fetchTopIds() async{

     final response = await client.get('$_baseUrl/posts?fields=id');
     final ids = json.decode(response.body); 
     return ids.cast<int>();
}

4 Answers 4

2

This should do what you want:

var intIds = ids.map<int>((m) => m['id'] as int).toList();
Sign up to request clarification or add additional context in comments.

2 Comments

thanks but this show me error that the method map is not defined.
I think I mixed something up in your code. Please check my updated answer.
2

This is what I did when got "array of object" as response (I know I'm late. But it will help the next guy)

    List list = json.decode(response.body);

  if (response.body.contains("id")) {
    var lst = new List(list.length);

    for (int i = 0; i < list.length; i++) {
      var idValue = list[i]['id'];
      print(idValuee);
    }

Comments

0

Here I attached a complete example of converting a list to JSON and then retrieve the List back from that JSON.

import 'dart:convert';

void main() {

  List<int> a= [1,2,3]; //List of Integer

  var json= jsonEncode(a); //json Encoded to String
  print(json.runtimeType.toString());

  var dec= jsonDecode(json); //Json Decoded to array of dynamic object

  print(dec.runtimeType.toString());


  a= (dec as List).map((t)=> t as int).toList(); //dynamic array mapped to integer List

  print(a);
}

Comments

0

I found a better and easier way to get required information/ data from JSON Array.

Refer to https://pub.dev/packages/http/example to know more

Code to get a quick starts:

void main(List<String> arguments) async {
var url = 'ENTER YOUR API ENDPOINT';
var response = await http.get(url);
var jsonResponse = convert.jsonDecode(response.body);
var itemCount = jsonResponse['totalItems'];
print('Number of books about http: $itemCount.');

Here 'totalItems' would be your desired key.This will only work if you get a response.statusCode == 200

5 Comments

But this does not deserialize JSON to list object. Count of items is not the only thing to do with list.
But If you have a json object {"book_name":"book1", "author_name","author1"}. You can access value of "book_name" and value of "author_name" i.e "book1" and "author1" respectively.
Yes, but the question was to turn JSON object with array of objects with id field to list of that same ids, not how to access single object property.
This you tube tutorial might help you achieve your desired goal youtu.be/LQAUmQFViAY
SO is not a forum, so it's not useful to provide links to some youtube tutorials, especially when there's a good answers with code snippets. I do not need to achieve something, the Questioner asked for the very precise task and expects to have answers for the question, but this is not about the OP's task. You can check this article for more info: How do I write a good answer?.

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.