DEV Community

Cover image for DART FUNCTIONS YOU NEED TO KNOW.
Areeba Farooq
Areeba Farooq

Posted on

2 1

DART FUNCTIONS YOU NEED TO KNOW.

Functions are the building blocks of readable, maintainable, and reusable code. A function is a set of statements to perform a specific task.

🌟ForEach( ):

Iterate through entire elements in the list.

Use it when you need to scan all the elements and certain actions over individual elements.

List<String> perfumeBrand =
[ "claira", "Blue wales", "Vagas" ];
perfumeBrand.forEach((brand) => print(brand));

//Output
claira
Blue wales
Vagas
Enter fullscreen mode Exit fullscreen mode

🌟Sort( ):

Sort the elements in the list.

Use it with the combination of the reversed() method to filter data in the list.

List<int> nums = [ 1-, 6, 8 ];
nums.sort( );

//Output ==> [-1, 6, 8]

**************************************************************

List<String> number = [ "Four", "Six", "Two" ];
number.sort((x, y) => x.length.compareTo(y.length));

//Output ==> [Two, Four, Six]
Enter fullscreen mode Exit fullscreen mode

🌟Skip( ):

Skips defined items from any List.

Use it when you need to remove any blacklisted element from an array of items (from your list).

int elementToSkip = 3;
List<int> elements = [1,2,3,4,5,6,];
List<int> skippedList = elements.skip(elementToSkip).toList();

//Output ==> [1,2,4,5,6,]
Enter fullscreen mode Exit fullscreen mode

🌟ToSet( ):

Removes duplicates and iterates with the same items.

Use it when don’t need users having the same email address or if you need to add unique elements.

List<int> nums = [1,1,4,5,4,];
nums.toSet( ).toList( ).sort( );

//OUTPUT ==> [1,4,5]
Enter fullscreen mode Exit fullscreen mode

🌟Any( ):

Scans the elements in the list & produces boolean output depending on the provided condition.

List<String> perfumeBrand =
[ "claira", "Blue wales", "Vagas" ];
perfumeBrands.any(brand) => brand.startsWith("B"));

//Output ==> TRUE

perfumeBrands.any(brand) => brand.startsWith("ht"));

//Output ==> TRUE
Enter fullscreen mode Exit fullscreen mode

🌟Shuffle( ):

Arranges elements in a random order/ sequence.

List<int> nums = [2,4,6,8,10];
nums.shuffle( );

//Output ==> [10,2,6.4.8]
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay