check library

A simple set of pre/post-condition checkers based on the Guava Preconditions class in Java.

These checks are stronger than 'assert' statements, which can be switched off, so they must only be used in situations where we actively want the program to break when the check fails.


Performance may be an issue with these checks if complex logic is computed in order to make the method call. You should be careful with its use in these cases - this library is aimed at improving maintainability and readability rather than performance. They are also useful when the program should fail early - for example, null-checking a parameter that might not be used until the end of the method call.

Error messages

The message parameter can be either a () => Object or any other Object. The object will be converted to an error message by calling its toString(). The Function should be preferred if the message is complex to construct (i.e., it uses String interpolation), because it is only called when the check fails.

If the message parameter is null or returns null, a default error message will be used.


checkArgument(bool expression, {dynamic message}) → void
Throws an ArgumentError if the given expression is false.
checkListIndex(int index, int size, {dynamic message}) int
Throws a RangeError if the given index is not a valid index for a list with size elements. Otherwise, returns the index parameter.
checkNotNull<T>(T reference, {dynamic message}) → T
Throws an ArgumentError if the given reference is null. Otherwise, returns the reference parameter.
checkState(bool expression, {dynamic message}) → void
Throws a StateError if the given expression is false.