122 lines
2.1 KiB
Plaintext
122 lines
2.1 KiB
Plaintext
== Why is this an issue?
|
|
|
|
In Dart, there's a concise syntax for constructors with init formals. You can declare a simple class with constructor this way:
|
|
|
|
[source,dart]
|
|
----
|
|
class Person {
|
|
|
|
String name;
|
|
int age;
|
|
|
|
Person(this.name, this.age);
|
|
}
|
|
----
|
|
|
|
By default, the type of the constructor parameter is assumed to be the same as the type of the field. In case of using `super.field`, the type is assumed to be the type of the parameter in super class constructor. Thus, there's no need to declare it explicitly.
|
|
|
|
=== Exceptions
|
|
|
|
The rule doesn't apply if the type of the parameter is different. This should be declared explicitly:
|
|
|
|
[source,dart]
|
|
----
|
|
class Parent {
|
|
Iterable<String> children;
|
|
|
|
Parent(this.children);
|
|
}
|
|
|
|
class Person extends Parent {
|
|
Person(List<String> super.children); // Compliant, because the type is different
|
|
}
|
|
----
|
|
|
|
== How to fix it
|
|
|
|
Remove the type before `this.field` or `super.field`.
|
|
|
|
=== Code examples
|
|
|
|
==== Noncompliant code example
|
|
|
|
[source,dart,diff-id=1,diff-type=noncompliant]
|
|
----
|
|
class Person {
|
|
|
|
String name;
|
|
int age;
|
|
|
|
Person(String this.name, int this.age);
|
|
}
|
|
----
|
|
|
|
==== Compliant solution
|
|
|
|
[source,dart,diff-id=1,diff-type=compliant]
|
|
----
|
|
class Person {
|
|
|
|
String name;
|
|
int age;
|
|
|
|
Person(this.name, this.age);
|
|
}
|
|
----
|
|
|
|
==== Noncompliant code example
|
|
|
|
[source,dart,diff-id=2,diff-type=noncompliant]
|
|
----
|
|
class Parent {
|
|
Iterable<String> children;
|
|
|
|
Parent(this.children);
|
|
}
|
|
|
|
class Person extends Parent {
|
|
Person(Iterable<String> super.children);
|
|
}
|
|
----
|
|
|
|
==== Compliant solution
|
|
|
|
[source,dart,diff-id=2,diff-type=compliant]
|
|
----
|
|
class Parent {
|
|
Iterable<String> children;
|
|
|
|
Parent(this.children);
|
|
}
|
|
|
|
class Person extends Parent {
|
|
Person(super.children);
|
|
}
|
|
----
|
|
|
|
== Resources
|
|
|
|
=== Documentation
|
|
|
|
* Dart Docs - https://dart.dev/tools/linter-rules/type_init_formals[Dart Linter rule - type_init_formals]
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
=== Message
|
|
|
|
Don't needlessly type annotate initializing formals.
|
|
|
|
=== Highlighting
|
|
|
|
Type annotation
|
|
|
|
'''
|
|
== Comments And Links
|
|
(visible only on this page)
|
|
|
|
endif::env-github,rspecator-view[]
|