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

passsy
passsy
Jan 11, 2019 · 4 min read
Image for post
Image for post

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.


Image for post
Image for post

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.

Image for post
Image for post
Image for post
Image for post

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

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

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade
A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store