How to use Bool in Flutter?

nzxcvbnm
3 min readJun 17, 2020

Bool variable was something that I though I had understood, but in the end I couldn’t use it smoothly. I tried, but it was very hard for me to write a sample of code using bool. Maybe I had not read a good definition? But one day, while scrolling through a piece of code in article I saw it, though — “that’s it”.

Also, I though bools are useless till my code grew up and it wasn’t as readable as before.

I prepared simple Flutter example and my description, just in case there was someone looking for more detailed explanation.

First of all, bool keeps values “true” or “false”. Yes or no. It says something has a specific feature or it doesn’t. There are 2 possible parts of code to execute depending on value, one part for “true” and one for “false”.

Bool variables often have typical names: isSomethingDone or isSomethingParticiple.

I quess you want some useful examples. Lately I was building a camera and very important bool was isCameraReady. When it was true, the app could start and when not — well, it couldn’t. If there was a gallery, I could use a bool isStorageFull, so if it was true, there would appear a message “Storage is full, delete some images”. Let’s say this gallery exists and it can contain max 100 pictures.

bool isStorageFull;
int images;
@override
Widget build(BuildContext context) {
if (images > 100) {
isStorageFull = true;
} else {isStorageFull = false;}
return Container(
child: isStorageFull ? Text("Delete some images") : Text("You can add more pictures")
);
}

First thing to start using bool is declaring it. It is okay to give it a default value or leave it empty, condition will be checked later and variable will get a proper value:

bool isStorageFull;

or

bool isStorageFull = false;

Next step is telling your app when bool changes it’s value. It all depends on condition in the circle brackets.

if (images > 100) {
isStorageFull = true;
} else {isStorageFull = false;}

Schema:

if (fullfilled condition) {
yourBool = true;
} else {yourBool} = false;

And the last. Why to do that? Why exactly you need this bool? Tell your app what happens when it is true and when it is false — it means, what your app should do when given above condition is fullfilled and when it is not. Here is part responsible for that:

child: isStorageFull ? Text("Delete some images") : Text("You can  add more pictures")
);

Question mark and colon is there for a reason, it splits part of the code depending on coder intentions.

child: yourBool ? <part called when bool is true> : <part called when bool is false>

Everything would work without variable, but magic of it is that, you can reuse the bool, which in the end makes code shorter. Also, it is more readable and when you come back to project after a week you easy know what is going on there.

It is good to skip bools in small projects, but after code get complex, you will thank yourself for keeping it readable.

To sum up:

  1. Declare your bool with name isSomethingParticiple or isSomethingDone. Example: isDeviceConnected, isCameraReady, isUserAdded.
  2. When your variable is true or false?
  3. What part of code you want to be executed when it is true and when it is false?

--

--