Skip to content
DeveloperMemos

Generating a random boolean with Flutter/Dart

Flutter, Dart, Math1 min read

Something that you might need to use at some stage during mobile development(Flutter, Native Android, Native iOS...) is a random boolean. I tend to use them sometimes with mobile ads in conjunction with frequency capping. If you've ever used Java before you're probably familiar with the following code snippet:

1Random random = new Random();
2random.nextBoolean();

The code above is pretty self expalanatory - it generates a random boolean. The other day I was searching for a way to do the same thing with Dart/Flutter. It's almost exactly the same:

1final random = Random();
2random.nextBool();

And to use the above class you need to make sure you're importing Dart's math package:

1import 'dart:math';