Make your Flutter projects more readable and effective with extensions!

Bedirhan Saglam
4 min readOct 23, 2022

--

Readability and practicality of the code is very important in Flutter. Today we will write a few extensions and see their use.

1.Extension for image files

We all add image files to our files. And many of us define them as Image.asset(‘assets/images/image.png’) . Instead, we can do this;

And we can use this extension as follows;

2.Extension for form validate and List operations

Suppose you have an email form and you want to make sure that the user has entered the “@” symbol for it. Or you have a list and you want to reverse it. Here is an extension for the easy way;

3.Extension for empty padding

With SizedBox, we can give space between two widgets. For example SizedBox(height:10). But you don’t want to see this everywhere, there has to be a simple way around it. Here it is;

And to use it, all we have to do is;

4.Extension for parse operations

There are times when you want to convert an int value to a double or one of those double expressions you want to convert an int value.

You can use it like this;

Extensions, that’s it for now. Now let’s write the expressions that we will use on many pages of our project as a function.

1.

I was using this structure in many places and I thought;
“Since I use it all the time, can’t I just make it easier?”
And I did.

2.

And when using;

3.

We may want to show some long texts as many characters as we want.

4.We also have a method to make the date format from the API suitable for displaying in the interface :)

Instead of writing them all over the place, we do them easily with a function and we can use them wherever we want.

5.We had a BLoC structure for authentication transactions. And we wanted to restrict some builds for non logged in user. Here is the function that makes it effective;

That’s all for now. Thank you for reading. Hope it will help you in your next project :)
You can also access this beautiful article of Veli Bacık, who inspired me to write this article, from the link below.

https://medium.com/flutter-community/season-two-flutter-short-but-golds-8cff8f4b0b29

--

--