Closed
Description
At runtime, including either an implicit or an explicit null assert message seems to behave correctly. In other words, both of these work as expected:
assert(false, null); // Failed assertion: ... 'false': is not true.
assert(false); // Failed assertion: ... 'false': is not true.
However, at build-time, asserts with an explicit null message will fail. I discovered this when I was building the following micro-library intended to make it easier for us to use build-time asserts;
https://github.com/cmc5788/global_assert#why-not-allow-an-assert-without-a-message
https://pub.dev/packages/global_assert
A minimal reproduction sample is as follows;
class BuildAssert {
const BuildAssert(bool condition, [String? message])
: assert(condition, message);
}
const _assert = BuildAssert(false);
void main() {}
Compiling this will fail with:
Context: Expected constant 'null' to be of type 'String', but was of type 'Null'.
- Dart SDK Version (
dart --version
)
Dart SDK version: 2.15.0-268.18.beta (beta) (Wed Nov 17 11:39:23 2021 +0100) on "macos_x64"