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 exist to help the developer, and to keep their code clean and readable. Readability does not equal performance optimization, so if you are looking to optimize your code, this is not the library for you.
Note: Dartlin tries to keep the performance of the provided methods as good as possible but they are not being benchmarked in any way. Most of the methods rely on closures to work.
Current libraries
Dartlin provides different libraries that you can import:
- dartlin/collections.dart
import 'package:dartlin/collections.dart';
- dartlin/control_flow.dart
import 'package:dartlin/control_flow.dart';
- dartlin/text.dart
import 'package:dartlin/dartlin.dart';
Or you can use the common library:
import 'package:dartlin/dartlin.dart';
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 List
s and Map
s. 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;
});
The iff
method is null-aware. This means that the type of the returned value from the block
is expected as a non-nullable type for the blocks of the elseIf
and orElse
methods.
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.
Libraries
- collections
- Adding methods for collection control.
- control_flow
- Adding control flow methods for writing more readable code.
- dartlin
- Dartlin is a helper library that provides readable methods with which you can write cleaner looking code.