Flutter Useful Extensions

Marcin Oziemski
The Startup
Published in
2 min readJun 17, 2020

--

Photo by Ryoji Iwata on Unsplash

I have an Android 🤖 dev background. So when I started to write in Flutter there were, of course, things that I could do in Kotlin but there were not possible in Dart. I’ve started to write my own extension that could bring a little bit of that back, but also others that help me in day to day coding.

This will be a couple of them. Let me know what do you think and maybe if you have your own and would like to share them in comments that would be great 😃

Kotlin

So in Kotlin you write let/apply/also/run etc. almost all the time. So what I came up with are implementations of let and also.

Example of usage:

In BlocListener you could do:

You would like to return filtered list but also assign it to the field:

Of course, “it” could be something more meaningful but for the sake of example, it will do.

List 🗄

In Flutter we use the lists on a daily basis, in widgets tree via children param or just a list of data.

Example of usage:

Ok, so the state (eg. in Bloc) should be immutable, why? Oh boy, that needs a separate explanation for example from here. But let go back on tracks, if the state is immutable then lists in it are too. So when we want to change them we need to create a copy of them and not just add/remove on them.

P.S. I know it’s a shallow copy and for purists out there, we should make a deep copy of all objects inside.

If we would like to do some custom avatar list like:

Then “mapIndexed” (which is also in Kotlin) is perfect:

Iterable

So to be more flexible I add also extensions for Iterable.

Example of usage:

ContainsAll is great when you want to for example implement filtering by many categories.

FindOrNull is just a shorter notation then using orElse with lambda.

That's all folks 😃

Those are just some of my extensions I’m using on a daily basis. If you liked it please leave 👏.
If you have your set that you can share please do in the comments.
Thank you for reading 👋.

--

--

Marcin Oziemski
The Startup

Senior Android and Flutter Developer. Working at Netguru.