GroupBy in Flutter

GroupBy in Flutter :

Flutter tutorial provide the group by.
What is group by in programming languages likes flutter, dart, android, java, kotlin etc.

What is groupby?

  • Groupby is single key and value listing for example Map.
  • Groupby different ways to created likes name, date, marks, area etc.

Flutter groupby:
Groups the elements in values by the value returned by key. Returns a map from keys computed by key to a list of all values for which key returns that key. The values appear in the list in the same relative order as in values.

1) pub.yaml add library
Collection:
2) Collection library in flutter provide own function groupBy() that is return Map>

groupBy in flutter explain:
1) key name: Kamlesh, Hirva
2) Value listing: That is return group data of list.

GroupBy in Flutter

GroupBy sorting in flutter:

studentList.sort((a, b) => a.name.compareTo(b.name));

output:
[
  {
    "name": "Kamlesh",
    "listing": [
      {
        "name": "Kamlesh",
        "marks": 10
      },
      {
        "name": "Kamlesh",
        "marks": 10
      }
    ]
  },
  {
    "name": "Hirva",
    "listing": [
      {
        "name": "Hirva",
        "marks": 10
      }
    ]
  }
]

import "package:collection/collection.dart";
import 'dart:convert';

main(List<String> args) {
  List<Student> studentList = [
    Student(name: 'Kamlesh', marks: 10),
    Student(name: 'Kamlesh', marks: 10),
    Student(name: 'Hirva', marks: 10)
  ];

  //studentList.sort((a, b) => a.name.compareTo(b.name)); Sorting

  var groupByList = List<StudentGroup>();

  groupBy(studentList, (Student student) => student.name).forEach((key, value) {
    groupByList.add(StudentGroup(name: key, listing: value));
  });

  print(json.encode(groupByList));
}

class Student {
  String name;
  int marks;

  Student({this.name, this.marks});

  Student.fromJson(Map<String, dynamic> json)
      : name = json['name'],
        marks = json['marks'];

  Map<String, dynamic> toJson() => {'name': name, 'marks': marks};
}

class StudentGroup {
  String name;
  List<Student> listing;

The flutter tutorial  is a website that bring you the latest and amazing resources of code. All the languages codes are included in this website. The languages like flutter, android, java,kotlin etc.with the help of this languages any user can develop the beautiful application

For more information about Flutter. visit www.fluttertutorial.in