kt.dart — Kotlin like collections for your Flutter business logic

passsy
passsy
Jan 11 · 4 min read

Dart’s missing high-level collections for business logic

The most common collection in nearly every programming language is the array. Dart doesn’t have arrays, Dart arrays are Lists. And Dart’s Lists are amazing compared to Java’s Array. But is such a comparison justifiable? Shouldn’t Dart’s List better be compared with Java’s ArrayList or Kotlins MutableList?

Explicit immutability

Using Kotlin made me used to immutability. My entities are immutable (data classes) and so are the Lists I return from my APIs. Immutable entities aren’t a problem in Dart. But a immutable List has the same API as a modifiable List. Consumers might expect to be able to mutate a immutable List. There is no compiler warning, it crashes at runtime.

Deep Equality

Comparing two Dart collections (List, Set or Map) doesn’t compare their contents, it only checks their identity. Dart offers a solution: The equality functions in the collection package.


kt.dart: collection

kt.darts collection package offers immutable collections (KtList, KtSet, KtMap) with a correct equals implementation.

KtIterable doesn’t extend Dart’s Iterable

Unlike built_value, kt.dart collections don’t extend Dart’s Iterable. The reason for this is that Dart’s naming of methods is uncommon. Modern languages (Kotlin, Swift, TypeScript) all named their Iterable methods the same (map,flatMap, filter, …). This helps a lot when working with developers from different platforms. Dart’s expand or where is less common.

150+ methods

Additionally, kt.dart collections comes with over 150 methods out of the box. All of them can be easily discovered via auto-completion.

Just the beginning

This is just the beginning of kt.dart. The collection module is the first (and most important) part. But I can already think about more modules. For example ranges,sequences or text.


Flutter Community

Articles and Stories from the Flutter Community

Thanks to Muhammed Salih Guler, Filip Hracek, and Björn Quentin.

passsy

Written by

passsy

Google Developer Expert for Flutter and passionate Android developer #kotlin working at @grandcentrix in Cologne

Flutter Community

Articles and Stories from the Flutter Community