Dart tool to convert anything to boolean, just like !! operator in JS

A simple tool (really simple, about 25 lines of code) to convert a dart object, including null, in a bool (true|false), quite similar to how the double NOT operator (!!) works in Javascript and Typescript.

Features

You can use it as an operator (~ or twice ~~), as an extension (property .asBool) or as a simple helper method (asBool(value)).

Warning The operator ~ doesn’t work with int values because is used for bit-wise negate operator, for int objects use the extension or helper method

What values are converted to false:

  • null
  • 0 And 0.0
  • double.nan “Not a Number” values
  • "" Empty Strings
  • [] Empty iterable objects
  • <any>.isEmpty == true Whatever object with a isEmpty property that returns true (i.e, a Map or any custom class that implement isEmpty)

All other values are considered as true.

Getting started

Add the package asbool to your project:

dart pub add asbool

Import the package:

import 'package:asbool/asbool.dart'; // All included

Usage

Use the tool as you wish

final foo = 'Hi';
final bar = [];
final m = {'a':'b'};

assert(~~foo == foo.asBool); // true == true
assert(asBool(foo) == ~~12); // true == true

assert(asBool(bar) == bar.asBool); // false == false
assert(asBool(0.0) == ~~double.nan); // false == false
assert(~~m == true); // true == true
assert(asBool({}) == false); // false == false

final things = [123, 'Hi', null, {}, {'a': 'b'}, double.nan, double.infinity, MyClass(), MyOtherClass(), 0, 0.0, ['ok']];
final trueThings = things.where(asBool).toList();
print(trueThings); // It will print: [123, 'Hi', {'a': 'b'}, double.infinity, MyClass(), ['ok']]

Additional information

If you have any suggestion or problem with the lib, please open an issue and I’ll try to help.

GitHub

View Github