prefer-asserts-in-initializer-lists
Same as prefer_asserts_in_initializer_lists in Dart lints
- Group
- Style
- Path
lint/style/prefer-asserts-in-initializer-lists- Default
- Off, opt in
- Origin
- Dart lints
- Rule
- 314 of 512
Flags leading constructor-body asserts that could live in the initializer list.
An assert at the start of a generative constructor body that does not
need the instance (no this, no instance member of the class or its
superclasses and mixins) can move into the initializer list, where it runs
before the object is used and works for const constructors too. Fields
initialized by this.x formals count as available, since the formal holds
the same value, as do fields a super.x formal passes to a superclass
constructor's this.x parameter. The analyzer retains the last class's
superclass/mixin chain for subsequent extension type constructors.
class A {
A(int a) { assert(a != 0); } // flagged
A(int a) : assert(a != 0); // ok
}
Invalid
int limit = 10;
class Range {
final int start;
final int end;
Range(this.start, this.end) {
assert(start <= end);
assert(end < limit);
print(start);
}
Range.of(int a, int b) : start = a, end = b {
assert(a <= b, 'ordered');
}
Range.single(int v) : start = v, end = v {
assert(isValid(v));
}
Range.list(List<int> items) : start = items.first, end = items.last {
assert(items.every((e) => e >= 0));
}
static bool isValid(int v) => v >= 0;
}
class Sub extends Range {
Sub(int a) : super(a, a) {
assert(a > 0);
}
}
class Parent {
final int value;
Parent(this.value);
}
class SuperFormal extends Parent {
SuperFormal(super.value) {
assert(value > 0);
}
}
extension type CheckedValue(int? value) {
CheckedValue.checked(this.value) {
assert(value != null);
}
}
class ParameterMessage {
ParameterMessage(int value) {
assert(value > 0, value);
}
}
class NestedAssertions {
NestedAssertions(int value) {
assert(() {
assert(value > 0);
return true;
}());
assert(value < 10);
print(value);
assert(value != 5);
}
} Range(this.start, this.end) { assert(start <= end); assert(start <= end); assert(end < limit); Range.of(int a, int b) : start = a, end = b { assert(a <= b, 'ordered'); Range.single(int v) : start = v, end = v { assert(isValid(v)); Range.list(List<int> items) : start = items.first, end = items.last { assert(items.every((e) => e >= 0)); Sub(int a) : super(a, a) { assert(a > 0); SuperFormal(super.value) { assert(value > 0); CheckedValue.checked(this.value) { assert(value != null); ParameterMessage(int value) { assert(value > 0, value); NestedAssertions(int value) { assert(() { }()); assert(value < 10);Valid
class Range {
final int start;
final int end;
int get length => end - start;
Range(this.start, this.end) : assert(start <= end);
Range.checked(this.start, this.end) {
assert(length >= 0);
}
Range.self(this.start, this.end) {
assert(this.start <= this.end);
}
Range.method(this.start, this.end) {
assert(validate());
}
Range.late(int a, int b) : start = a, end = b {
print(a);
assert(a <= b);
}
factory Range.make(int a) {
assert(a > 0);
return Range(a, a);
}
Range.other(Range other) : start = other.start, end = other.end {
assert(other.length >= 0);
}
bool validate() => start <= end;
}
class External extends Object with Mixed {
final int x;
External(this.x) {
assert(mixed > 0);
}
}
mixin Mixed {
int get mixed => 1;
}
class Parent {
final int value;
Parent(this.value);
}
class SuperFormal extends Parent {
SuperFormal(super.value) : assert(value > 0);
}
extension type CheckedValue(int? value) {
CheckedValue.checked(this.value) : assert(value != null);
}
class InstanceMessage {
InstanceMessage(int value) {
assert(value > 0, this);
}
InstanceMessage.property(int value) {
assert(value > 0, this.toString());
}
}
class InitializerClosure {
final bool value;
InitializerClosure(int input)
: value = (() {
assert(input > 0);
return true;
})() {
assert(input > 0, this);
}
}
class SuperClosureParent {
SuperClosureParent(bool value);
}
class SuperArgumentClosure extends SuperClosureParent {
SuperArgumentClosure(int input)
: super((() {
assert(input > 0);
return true;
})()) {
assert(input > 0, this);
}
}Configure prefer-asserts-in-initializer-lists
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"style": {
"prefer-asserts-in-initializer-lists": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/style/prefer-asserts-in-initializer-lists: <reason>More style rules
- prefer-asserts-with-message
prefer_asserts_with_messageFlags
assertstatements and assert initializers that have no message. - prefer-async-await
prefer-async-awaitFlags
.then(...)future chains that should useasync/await. - prefer-async-callback
prefer_async_callbackflutterFlags the type
Future<void> Function()in favor of Flutter'sAsyncCallback. - prefer-bloc-event-suffixbloc
Enforce the package-confirmed Bloc convention.
- prefer-bloc-extensionsbloc
Prefer context.read/watch over
package:flutter_bloc's provider lookups. - prefer-bloc-state-suffixbloc
Enforce the package-confirmed Bloc convention.
- prefer-center-over-alignflutter
Prefers
Centerover anAlignwhose alignment is known to be centered. - prefer-collection-literals
prefer_collection_literalsrecommendedFlags no-argument collection constructor calls that a literal would express.