Flutter list any

In this tutorial, we will learn how to check if a list contains a value in dart. There are multiple methods to achieve this. Dart provides us bunch of methods to achieve this in an efficient manner. Dart has the ability to search any kind of value inside a List whether it is an integer, string, map, or anything. Here are some of the Methods by which we can check if a list contains a value in Dart.

Check if the List contains a value

To check if a list contains a value Here are some methods given below.

Using List.contains[ ] Method

The List.contains[ ] is the recommended method to check if a value is present in a list or not. This method takes the value to be searched as a parameter and returns true if the item found otherwise returns false.

Example:

List myList = [1, "1", true]; var r1 = myList.contains[1]; var r2 = myList.contains["1"]; var r3 = myList.contains[true]; print[r1]; // true print[r2]; // true print[r3]; // true r1 = myList.contains[2]; r2 = myList.contains["2"]; r3 = myList.contains[false]; print[r1]; // false print[r2]; // false print[r3]; // false

Output:

true true true false false false

Using List.indexOf[ ] method

The List.indexOf[ ] method is used to find the index of the element in a List. This method returns in negative if the item is not present in the list. So we can say if the item is present in the list this method will return 0 or any positive number less than the length of the array and if the item is not present in the array it will return in a negative [-1].

Example:

List myList = [1, "1", true]; var r1 = myList.indexOf[1]; var r2 = myList.indexOf["1"]; var r3 = myList.indexOf[true]; // Print true if the returned value // is greater than or equal to 0 print[r1 >= 0 ? true : false]; // true print[r2 >= 0 ? true : false]; // true print[r3 >= 0 ? true : false]; // true r1 = myList.indexOf[2]; r2 = myList.indexOf["2"]; r3 = myList.indexOf[false]; // Print false if the returned value // is greater than or equal to 0 print[r1 >= 0 ? true : false]; // false print[r2 >= 0 ? true : false]; // false print[r3 >= 0 ? true : false]; // false

Output:

true true true false false false

Using List.any[ ] method

The List.any[ ] method is used for checking if any one of the items on the list satisfies a test. This method returns true if any single element passes the test and returns false if no element passes the test.

Example:

List myList = [1, "1", true]; var r1 = myList.any[[item] => item == 1]; var r2 = myList.any[[item] => item == "1"]; var r3 = myList.any[[item] => item == true]; print[r1]; // true print[r2]; // true print[r3]; // true

Output:

true true true

Using dart loops

Loops for iterating a list [array] are a universal solution in every language. So here we also use the loops to iterate the array and check for a specific item in an array.

Using for loop

Here is the solution of checking an item in a list using for…loop.

Example: List myList = [1, "1", true]; for[int i = 0; i < myList.length; i++]{ switch [myList[i]]{ case 1: print["1 is present in List"]; break; case "1": print['"1" is present in List']; break; case true: print["true is present in List"]; } } Output: 1 is present in List "1" is present in List true is present in List

Using for..in loop

Here is the solution of checking an item in a list using for…in loop.

Example: List myList = [1, "1", true]; for[dynamic i in myList]{ switch [i]{ case 1: print["1 is present in List"]; break; case "1": print['"1" is present in List']; break; case true: print["true is present in List"]; break; } } Output: 1 is present in List "1" is present in List true is present in List

Using While loop

Here is the solution of checking an item in a list using while loop.

Example: List myList = [1, "1", true]; int i = 0; while[i < myList.length]{ switch [myList[i]]{ case 1: print["1 is present in List"]; break; case "1": print['"1" is present in List']; break; case true: print["true is present in List"]; break; } i++; } Output: 1 is present in List "1" is present in List true is present in List

Background photo by XiaoXiao Sun on Unsplash

As part of my venture into client-side application development with Dart, I began exploring the way one could go about working with Lists or Array types. Aside from the documentation being comprehensive, I was also able to find support on the StackOverflow community and successfully achieved what was needed.

Continue reading on my blog

In today’s article we’ll be looking at the “batteries-included” notion of Dart, in particular the inbuilt utilities for working with Lists. I’ve hand-picked 10 of the most common ones you show know for your next app. I’ve also prepared the example snippets so you could play with those yourself 😁

So, shall we begin?

Runs a function on each element in the list

2. map[]

Produces a new list after transforming each element in a given list

3. contains[]

Checks to confirm that the given element is in the list

4. sort[]

Order the elements based on the provided ordering function

5. reduce[], fold[]

Compresses the elements to a single value, using the given function

6. every[]

Confirms that every element satisfies the test

7. where[], firstWhere[], singleWhere[]

Returns a collection of elements that satisfy a test.

firstWhere[] returns the first match in the list, while singleWhere[] returns the first match provided there is exactly one match.

8. take[], skip[]

Returns a collection while including or skipping elements

9. List.from[]

Creates a new list from the given collection

As of Dart 2.0, the new keyword is optional when instantiating objects.

10. expand[]

Expands each element into zero or more elements

Conclusion

I hope this has been insightful and if this is your first exposure to Dart, read my first steps tutorial to grasp the basics. The example snippets for this article are available on DartPad.

I also run a YouTube channel teaching subscribers to develop full-stack applications with Dart. Become a subscriber to receive updates when new videos are released.

Like, share and follow me😍 for more content on Dart.

And this concludes the tutorial. Thanks for reading.

Read more Dart tutorials on my blog

What to check out next

✉️ Subscribe to CodeBurst’s once-weekly Email Blast, 🐦 Follow CodeBurst onTwitter, view 🗺️ The 2018 Web Developer Roadmap, and 🕸️ Learn Full Stack Web Development.

Video liên quan

Chủ Đề