Either way!

Jveena
3 min readAug 26, 2021

--

Either way, you agree that Flutter developers are in high demand these days, and that’s true when the developers’ skills are not merely as deep as the skin, but deep enough to distinguish Dart as a language, independent of Flutter.

Many of these in-demand developers are advocates of pure functions that avoid concepts of shared states. Yes, I am referring to those Functional Programming wizards!

The solution to bring all the goodness, the power, and the inherent stability of functional programming into Flutter is to use the Dartz package.

Get it here : https://pub.dev/packages/dartz/install

Let’s dive into the Dartz package and zoom into one of its most sought-after utilitarian features — the Either<L, R> class.

Going by the name, Either type can hold one or the other value.

If we define a function that returns an int, we expect it to return an integer value. That’s a given. However, if a function is defined with an Either<int, string> as its return type, then the function is supposed to return either an int or a string; never none; never both.

The following illustration helps me to visualize an Either type:

Either<int, string> type can be visualized as:

Either<int,string>

This will hold either an int or a string; never none; never both.

At this point, an example would add clarity.

A code snippet demonstrating the use of Either type:

Code Snippet- 1

By the way, the above code is from one of my flutter projects following the Domain Driven Design principles. All thanks to ResoCoder Youtube channel. I highly recommend this play list in which he explains everything from Dart to Flutter to Domain Driven Design.

Either<ValueFailure<String>, String> validatePassword(String input) {        if (input.length >= 6) {            return right(input);        } else {          return left(ValueFailure.shortPassword(failedValue: input));       }}

In above code, the function validatePassword() is to validate an input String and it returns either a ValueFailure object (left) or a String (right). Of course, it never returns none. And never both!

Have a close look at the return statements and the use of right and left. In this example, if the length of the password string is greater than 6, we return a value to the right of Either, which is the password itself, otherwise we return a Failure object to the left of Either.

Elegance!

This guarantees that the function would return a Failure type, or a valid password string.

I like to visualize it like this:

I will conclude with the reason for writing about Dartz:

I have tasted the elegance of functional programming in dart by using the Either type. Either type is just a tiny aspect of what is possible with Dartz. Yet for me, this was my first real experience in coding something classic to this level.

I am a Flutter enthusiast, however I realize that it is only when one moves closer to Dart, we start to attain insights into the real capabilities of what Flutter can do for us.

Happy Flutter coding everyone!

--

--