Assertions in Dart class constructors
Not to be confused with assertions done in testing, class constructor assertions are a great way to prevent errors in your Dart code. They can be used to check that the values passed to a constructor are valid as per application logic, for example, ensuring that an integer passed is within a given range.
To use an assertion in a class constructor, you simply add an
assert() statement to the constructor body (examples below). The
assert statement takes a boolean expression as its argument, and if the expression evaluates to
false, the constructor throws an
AssertionError exception. By default, a generic exception message will be shown, but if a second parameter is passed to
assert then that will be used as the error message instead.
Here is a contrived example of a simple class constructor with an assertion:
This constructor will throw an
AssertionError exception if the
name parameter is empty.
Here is an example of how to use the
Assertions can be used to check all sorts of things in class constructors. For example, you can use them to check that:
- The values passed to the constructor are within a certain range.
- The values passed to the constructor are of the correct type.
- In cases where arguments A and B are both not mandatory, at least one has a value
- Think of an Avatar widget, where you could either have a letter or an image
Note however, that assertions only happen in development code and do not run in production code at all (see here).
Things to consider
- Don't use assertions to perform complex operations. Assertions are meant to be simple checks, not full-blown validation functions.
- Don't use assertions to replace unit tests. Assertions are a good way to catch potential errors early on, but they are not a substitute for unit tests
Examples of usages in Material Design widgets
Flutter's Material Design widgets make extensive use of assertions in their constructors which are really good at moaning at you, especially in the early days of learning Flutter. For example, the
Container widget has an assertion to check that if you pass
decoration, that you also do not set a