Supercharge Your Dart Code with Dartx and Time Extensions 🚀

Vinayak
YavarTechWorks
Published in
2 min readFeb 1, 2024

Dartx and Time extensions are here to turbocharge your Dart development experience. These powerful extensions, easily integrated into your projects, bring a myriad of functionalities, making your code more concise and expressive. Let’s dive into the magic they offer!

Getting Started with Dartx 🎉

To kick off the party, add Dartx to your pubspec.yaml:

dependencies:
dartx: any

Once you’ve invited Dartx, import the library into your Dart files:

import 'package:dartx/dartx.dart';

Now, let’s explore some of the exciting extensions Dartx has to offer:

Iterable Extensions

.slice()

Extract elements from indices start to end:

final slice = [1, 2, 3, 4, 5].slice(1, -2); // [2, 3, 4]

List Extensions

.sortedBy() & .thenBy()

Sort lists by multiple properties:

final dogs = [
Dog(name: "Tom", age: 3),
// … other dogs
];
final sorted = dogs
.sortedBy((dog) => dog.name)
.thenByDescending((dog) => dog.age);

.distinctBy()

Get distinct elements from a list:

final list = ['this', 'is', 'a', 'test'];
final distinctByLength = list.distinctBy((it) => it.length);
// ['this', 'is', 'a'] 'test' is ignored as 'test' lenght and 'this' lenght are same

.flatten()

Flatten nested collections:

final nestedList = [[1, 2, 3], [4, 5, 6]];
final flattened = nestedList.flatten(); // [1, 2, 3, 4, 5, 6]

String Extensions

.capitalize() & .decapitalize()

Manipulate string capitalization:

final word = 'abcd'.capitalize(); // Abcd
final anotherWord = 'Abcd'.capitalize(); // Abcd
final word = 'abcd'.decapitalize(); // abcd
final anotherWord = 'Abcd'.decapitalize(); // abcd

.isBlank() & .isNotBlank()

Check if a string is blank or not:

final notBlank = ' .'.isBlank; // false
final blank = ' '.isBlank; // true

.isDouble() & .isInt()

Check if a string is a double or an integer:

final a = ''.isDouble; // false
final b = '1'.isDouble; // true
final c = '123'.isInt; // true

And Many More…

Dartx provides a plethora of extensions for various data types. Now, let’s turn our attention to another star of the show — Time extensions!

Introducing Time Extensions ⏰

Dartx seamlessly integrates with the time package to bring you powerful time-related functionalities. Here’s a glimpse:

int secondsInADay = 1.days.inSeconds;
Duration totalTime = [12.5.seconds, 101.milliseconds, 2.5.minutes].sum();
DateTime oneWeekLater = DateTime.now() + 1.week;

These time extensions, powered by the time package, open up a world of possibilities for handling durations and date/time calculations effortlessly.

Find more on my previous blog or explore the package directly on pub.dev.

Wrapping It Up 🎁

Dartx and Time extensions are your secret weapons for cleaner, more expressive Dart code. They eliminate boilerplate, reduce complexity, and make your codebase a joy to work with.

So, don’t miss out on this coding fiesta — integrate Dartx and Time extensions into your Dart projects today and experience the magic firsthand! 🌟

Thanks for reading this article ❤

--

--