Looking For Anything Specific?

ads header

The Best List Handling Methods in Flutter And Dart

The Best List Handling Methods in Flutter And Dart

We can use a variety of methods in a list, but we’ll look at how we can use a list in a flutter to achieve whatever we want and common ways to use a list effectively.

forEach method

When calling this function, we should pass an anonymous void function as an argument (so it doesn’t return any value).

Let us look at some examples.

As you can see, when we call the foreach method, it applies the function inside the parenthesis for each iteration.

another example

Here we created the “Feelings” model that we wanted, If you started studying flutter you would have created a lot of models. hahahhahaah….. Okay, in this section, we wrote a model that takes the name of the feeling and its color, In our main method we created a list .then we put data as the datatype of our model which we created

I also reinforce that we can only add a void function inside the foreach method. That means you won’t be able to return anything from the function.

Map method

This method returns a representation of the mapped elements. After calling the map function, we must basically pass the anonymous function inside the parenthesis.

That function basically tells us what transformations we can do for each iteration, and we can return anything in that function, so in fluter, we can return widgets that our aspects have. At the end of calling the map function, we must convert these maps to a list using.toList * This is essential when calling the map function in Dart.

Consider the following example:

I’ve made a list of Pen types, and I need to separate or map the prices from the list in order to make a new list of prices.

reduce Method

Reduces a collection to a single value by iteratively combining elements of the collection using the given function. This method starts with the first element from the iterator, and then combines it with the remaining elements in iteration order,

In this case, we pass two arguments to the anonymous function: one is “value” and the other is “element”. The collection of single variable values is argument value. the element represents the current iteration, we can perform any function with it and finally return value (argument value)

ok let's dive deeper inside of reduced method what happened

This is also the same code as what will happen inside the reduced function. This is an informal representation of reduce method

Here’s another example of how to use a reduced function, and you can learn more about it here.

where Method

This is similar to a filter method.

Returns a list of elements that satisfy our requirements.

We wrote an anonymous function to return the bool value; the function is going to check all elements of our list to see if the specific condition exists, and if it is, it returns the elements.

Let’s look at a real-world example.

Expand Method

Expands each element by using iterable. We should return a list by doing any operation on that element inside the anonymous function; here, we return a list so you can return more elements so we can expand the element.

Thanks for reading this article!!! Hope you will enjoy.
Leave a comment below or ask me via twitter if you have any questions

Post a Comment

0 Comments