How to Split a object list to two sublists based on condition in dart or flutter.

 we can use a simple ceil() function in Flutter for divided one list into two other lists like here 

To split an object list into two sublists based on a condition using .ceil(), you can use the List.partition() method. This method takes a predicate function as an argument and returns two sublists: one containing elements that satisfy the condition and another containing elements that do not.

Full Code:

if you have categoryList data. which is Model type.

void getCategory() async {
try {
categoryList.value = await CourseRepository().getCategory();
if(categoryList.value.isNotEmpty){
var ca_list = categoryList.value.length/2;
List<List<CategoryModel>> chunks = chunkFp(categoryList.value, ca_list.ceil());
categoryList2.value = chunks[0];
categoryList3.value = chunks[1];

}
} catch (e) {}
}
List<List<CategoryModel>> chunkFp(List<CategoryModel> arr, int size) {
int chunks = (arr.length / size).ceil();
return List.generate(
chunks,
(i) => arr.skip(i * size).take(size).toList()
);
}

 Details:  In Dart programming, you may come across situations where you need to split a list of objects into two separate sublists based on a condition. One way to achieve this is by utilizing the `List` class methods and the `ceil()` function from the `dart:math` library. This article will guide you through the process of splitting an object list into two sublists using `ceil()` in Dart.


## Table of Contents


1. Introduction to Splitting a List in Dart

2. The ceil() Function

3. Splitting an Object List

4. Example Code

5. Conclusion

6. FAQs


## Introduction to Splitting a List in Dart

Splitting a list is a common operation in programming, and Dart provides several methods to achieve this. In this article, we will focus on using the `ceil()` function to split an object list into two sublists based on a condition.


## The ceil() Function

The `ceil()` function is a mathematical function available in the `dart:math` library. It takes a numeric value as input and returns the smallest integer greater than or equal to that value. This function is useful when you want to divide a list into two sublists and ensure that the division is evenly distributed.


## Splitting an Object List

To split an object list into two sublists based on a condition, you can follow these steps:


1. Create an empty list to hold the first sublist.

2. Create another empty list to hold the second sublist.

3. Iterate over each object in the original list.

4. Check the condition for each object.

5. If the condition is met, add the object to the first sublist; otherwise, add it to the second sublist.


By using the `ceil()` function, you can calculate the number of objects that should be in each sublist to ensure an even distribution.


## Example Code

Here's an example code snippet that demonstrates how to split an object list into two sublists based on a condition using `ceil()` in Dart:


```dart

import 'dart:math';

void main() {

  List<int> originalList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

  List<int> firstSublist = [];

  List<int> secondSublist = [];


  int midIndex = (originalList.length / 2).ceil();

  for (int i = 0; i < originalList.length; i++) {

    if (i < midIndex) {

      firstSublist.add(originalList[i]);

    } else {

      secondSublist.add(originalList[i]);

    }

  }

  print('First Sublist: $firstSublist');

  print('Second Sublist: $secondSublist');

}

```

In this example, we have an original list of integers from 1 to 10. We calculate the `midIndex` by dividing the length of the original list by 2 and applying the `ceil()` function to ensure an even split. We then iterate over each element in the original list and add them to the appropriate sublist based on the condition.


When you run this code, you will see the output:

```

First Sublist: [1, 2, 3, 4, 5]

Second Sublist: [6, 7, 8, 9, 10]

```

The original list has been split

 into two sublists, with the first sublist containing the first five elements and the second sublist containing the remaining five elements.

## Conclusion

Splitting an object list into two sublists based on a condition using `ceil()` in Dart can be accomplished by following the steps outlined in this article. By utilizing the `dart:math` library and the `ceil()` function, you can evenly distribute the objects between the sublists. This technique can be useful in various scenarios where you need to divide and process a list of objects based on specific conditions.


## FAQs

**Q1: Can I split a list into more than two sublists using this approach?**

The approach described in this article splits a list into two sublists. If you want to split a list into more than two sublists, you would need to modify the code accordingly. You can create additional empty lists and adjust the conditions in the iteration accordingly.

**Q2: Can I split an object list based on a custom condition other than index?**

Yes, you can modify the code to split the object list based on a custom condition other than index. Simply replace the condition in the `if` statement with your desired condition. For example, you can split the list based on the value of each object or any other criteria that suits your needs.

 

Post a Comment

Thank you

Previous Post Next Post