dartlin 0.4.1
dartlin: ^0.4.1 copied to clipboard

Dartlin is a helper library that provides readable methods with which you can write cleaner looking code.

plugin version coverage report pipeline status dependencies


Introduction #

Dartlin is heavily inspired by Kotlin, therefore Dartlin tries to bring the ease of data manipulation and clean code from Kotlin to Dart.

What it does #

Dartlin is a helper library that provides the following concepts:

  • Writing "cleaner" code by adding readable control-flow methods that are able to replace the standard Dart control-flow statements. See Control flow for more information.
  • Adds extension methods to existing Dart types for better and easier data manipulation. See Data manipulation for more information.
  • Provides methods to easily create data sets of certain types.

What it does not do #

Dartlin is not a performance library, the methods it provides are made to help the developer, and to keep their code clean and readable. And readability does not always equal performance optimization. So if you are looking to optimize your code, this is not the library for you.

Note: We do try to keep the performance hit of the provided methods at a minimal but we do not benchmark them in any way. Also most of the methods rely on closures to work.

Common Usage #

Control flow #

The Dartlin control_flow library provides methods for writing cleaner and more controllable code, these control flow methods can be used as a replacement for existing control flow statements or in combination with them.

Note: Dart already provides a few interesting control flow operations for Lists and Maps. So before you decide to use Dartlin, first read more about collection operators. It may already suit your needs.

if-statement as expressions

The iff method works like the if statement, but with the added bonus of being able to write them as expressions. You can use them to replace complex ternary operators with a readable if-like statement:

final x = iff(a < b, () {
  return a;
}).elseIf(a == b, () {
  return 0;
}).orElse(() {
  return b;

One caveat is that it will assume the if statement failed when a null is returned. But that does allow you to use the elseIf and orElse in combination with any other method that could potentially return a null value.

See the iff docs for more information.

switch-like statements as expressions

The when method replaces the switch statement. And can be used to write expressions:

final result = when(place, {
  1: () => CompetitionPlace.first,
  2: () => CompetitionPlace.second,
  3: () => CompetitionPlace.third,
  [4,5]: () => CompetitionPlace.honourableMentions,
  orElse: () => CompetitionPlace.others

See the when docs for more information.

try-statement as expressions

The tryy method works like the tryy statement, but with the added bonus of being able to write them as expressions. Allowing you to catch multiple exceptions and depending on those exceptions return different values:

final x = tryy(() {
  return aMethodThatCouldFail();
}, catches: {
  On<SomeException>: () {
    return 1;
  On<OtherException>: () {
    return 2;

See the tryy docs for more information.

Data manipulation #

Creating progression ranges

The range method allows the user to easily create a lists of certain num types. While being able to define the start value, the end value, the steps it should take to reach the end value and if it should be created in reverse or not:

// [0, 1, 2, 3, 4]
final list1 = range(0, to: 4);

// [4, 3, 2, 1, 0]
final list2 = range(4, downTo: 0); 

// [1, 3, 5, 7]
final list3 = range(1, to: 8, step: 2);

// [1, 2, 3]
final list4 = range(0, until: 4);

See the range docs for more information.

Development and Contributing #

Interested in contributing? We love merge requests! See the Contribution guidelines.

pub points



Dartlin is a helper library that provides readable methods with which you can write cleaner looking code.

Repository (GitLab)
View/report issues


API reference




Packages that depend on dartlin