0

I want groupBy of famousAs of this data.

I got my data from api something like this

Personality:[
  0: {
      "FullName":"Harry Potter",
      "DateOfBirth": "2020/02/16",
      "Department":"Branch Operation",
      "BirthDay":"Friday"
      "famousAs":"Actor"
   },
  1: {
      "FullName":"John Wick",
      "DateOfBirth": "2020/02/16",
      "Department":"Finance",
      "BirthDay":"Friday"
      "famousAs":"Actor"
   },
  2: {
      "FullName":"Priyanka Chopara",
      "DateOfBirth":2020/02/19,
      "Department":"Loan",
      "BirthDay":"Monday"
      "famousAs":"Actress"
   }
]

when i check ,type of this data then it is showing List of dynamic

Deepak
  • 1,664
  • 3
  • 19
  • 52
  • 1
    *"I want groupBy of famousAs of this data."* - use [groupListsBy](https://api.flutter.dev/flutter/package-collection_collection/IterableExtension/groupListsBy.html) – pskink Sep 08 '21 at 07:31

1 Answers1

0

If your want to group list of data according to their property value.

You can use .where() method to filter out the list.

List filter(List items, String value) =>
    items.where((element) => element['famousAs'] == value).toList();

First the api response data from your question is invalid.

If your List is same as following above filter function will work fine.

or if your api response is HashMap change to Map first

[
  {
    "FullName": "Harry Potter",
    "DateOfBirth": "2020/02/16",
    "Department": "Branch Operation",
    "BirthDay": "Friday",
    "famousAs": "Actor"
  },
  {
    "FullName": "John Wick",
    "DateOfBirth": "2020/02/16",
    "Department": "Finance",
    "BirthDay": "Friday",
    "famousAs": "Actor"
  },
  {
    "FullName": "Priyanka Chopara",
    "DateOfBirth": 2020 / 02 / 19,
    "Department": "Loan",
    "BirthDay": "Monday",
    "famousAs": "Actress"
  }
]

Usage..

final actors = filter(items, 'Actor');
final actresses = filter(items, 'Actress');
OYHP
  • 66
  • 5