Home > Mobile >  Flutter how to find how many values map has
Flutter how to find how many values map has

Time:08-21

I have an API, every time when I call it. It returns with a different number of keys.

Sometimes:

{
   "json":[
      "example",
      "example1"
   ]
}

Other times:

{
   "json":[
      "example",
      "example1",
      "example2"
   ]
}

How can I count how many are there in a map?

CodePudding user response:

once you get your response. you can use

  final map = jsonDecode(data) as Map<dynamic, dynamic>?;

  print(map?.length); // items on root 1

  final response = jsonDecode(data)["json"] as List?;
  print(response?.length); 
  • Related